0

I am working on a website (new to web development) and I am having an issue with the following code(setting the end date to tomorrow if it is blank):

var date = new Date($('#txtStartDate').val());

var tomorrow = date.getDate() + 1;

$('#txtEndDate').val(tomorrow);

Later in the same function

var myObj = {StartDate: $('#txtStartDate').val(), EndDate: $('#txtEndDate').val() };

I am using MVC 5 and when the object hits the controller the start date is set but the end date is still null (also using visual studio 2013 update 1 so debugging is broken since the js is in the cshtml file).

From searching, the code should work unless I have a mistake I am not seeing.

EDIT:

Well after finding a way to debug my code I found that $('#txtStartDate').val() was returning just mm/dd/yyyy and not providing the correct date. That is fixed. As far as not checking for the end date being null, there are 2 different conditions on the form where an end date could be null. For one I need to set the end date to be the next day from the start date, for another a null end date is perfectly valid.

  • `date.getDate()` just gets a date in the month, like `10`, is that what you want the value to be ? – adeneo Jan 28 '15 at 20:07
  • I don't think you can do `getDate() + 1` and have it go to the next day, as it wouldn't be defined. – Greg Jan 28 '15 at 20:08
  • Maybe a bit overkill but you could use http://momentjs.com/. Its really nice to use when wanting to deal with dates. – mattfetz Jan 28 '15 at 20:10
  • @adeneo this code should work based on http://stackoverflow.com/a/19691491/3302585 – RubberChickenLeader Jan 28 '15 at 20:10
  • @Greg If the start date was null I would fully expect the code to fail. The fact that start date is not null is when the issue happens. Start Date is set by the user but there are some cases where end date needs to be the next day (if statement above the first code section, it is getting hit because other code in the if statement is being executed correctly). – RubberChickenLeader Jan 28 '15 at 20:12
  • @WindRaven - that code is completely different, it writes it back to a date object using `date.setDate( date.getDate() + 1 )`, you're just returning a single number, not a date object, and a date object wouldn't be valid as a value anyway. What do you expect as the value, a timestamp, a string in a certain format etc. surely it's not supposed to be just a single number – adeneo Jan 28 '15 at 20:20
  • @adeneo you're correct, but that doesn't explain why it's returning `null`. @WindRaven can you post the rest of the code related to this? It looks like something else might be interfering if this isn't all the code... – rogMaHall Jan 28 '15 at 20:24
  • @rogMaHall full code is posted. The only thing missing is a couple more items in the object. – RubberChickenLeader Jan 28 '15 at 20:55
  • @WindRaven Are you sure that if statement is going through? It looks to me like it's not diving into it, leaving the end date null. – rogMaHall Jan 28 '15 at 21:06
  • @rogMaHall to make sure I intentionally leave chkBox unchecked and when my object reaches the controller it is checked (the bool is set to true). – RubberChickenLeader Jan 28 '15 at 21:10
  • @WindRaven You're using `$('#chkBox').prop('checked')` in the object you create, which changes the value of the checkbox to 'checked', so it will always be checked when you reach the controller. Use `$('#chkBox').attr('checked')` instead. And use a `console.log()` or an `alert()` in the if statement instead. I'm pretty sure it's not diving into it... – rogMaHall Jan 28 '15 at 21:15
  • @rogMaHall I am only coming in and modifying the code. Know C# but this is my first foray into JS. The code seems to work and from what I have read `$('#chkBox').prop('checked')` pulls back the value but `$('#chkBox').prop('checked', true)` would set it. This however has been a fun foray into the unknown. The date code is my code but it could be broken as hell and I would have no idea. – RubberChickenLeader Jan 29 '15 at 03:26

1 Answers1

0

Assuming var date = new Date($('#txtStartDate').val()); returns a valid date, and assuming its todays date (29th January 2014), then

var tomorrow = date.getDate() + 1; // returns 30

will return 30 which cannot be bound to DateTime? so the value in the controller will be null.

Its not clear what the point of IsChecked: $('#chkBox').prop('checked') is - it will always return true so you may as well just omit it.

Based on your statement "set the end date to tomorrow if it is blank" (which your code is not even testing), your code should be

var startDate = $('#txtStartDate').val();
var endDate = $('#txtEndDate').val();

if (!endDate) // test is the end date has been entered
{
  var tomorrow = new Date()); // returns Jan 29
  tomorrow.setDate(tomorrow.getDate() + 1); // returns Jan 30
  // assumes MM/dd/yyyy format
  $('#txtEndDate').val(tomorrow.getMonth() + 1 + '/' + tomorrow.getDate() + '/' + tomorrow.getFullYear());
  endDate = $('#txtEndDate').val();
}

var myObj = { StartDate: startDate, EndDate: endDate  };

$.ajax({
  url: '@Url.Action("MvcCall", "My")', // dont hard code urls!!
  type: 'POST',
  contentType: 'application/json; charset=utf-8',
  data: myObj,
  success: function (data) {
    ....
  }
});

Note if the txtStartDate and txtEndDate elements are just textboxes, then you will want additional checks to ensure their values are valid dates

And learn to debug your client side code. Hit F12 and put a breakpoint on the script and inspect the values or add console.log(yourVar); statements

  • Thanks for the answer. I will try out the code later this morning. As far as most of this its a maintenance job adding slight additional functionality to a form that is all ready coded. There are all ready several things I want to refactor when I have the time. Right now things are getting left the way they are to get to prod. – RubberChickenLeader Jan 29 '15 at 13:42
  • for debugging there is an annoying little bug (that was fixed in VS 2013 update 2 I have update 1) that prevents breakpoints from being hit if the JS is in a .cshtml file instead of a .js file. – RubberChickenLeader Jan 29 '15 at 13:43
  • I was referring to client side debugging using your browsers tools (nothing to do with VS) –  Jan 29 '15 at 23:08