0

Here's the problem: I have a Kendo.DatePicker, acting as a filter, and it's default value is set as this, in the control

.Value(DateTime.Now.AddMonths(-1))

And when we click in a 'Clear Filters' button, I want the date to return to the default value, which is always CurrentDate - 1 month.

I have this, to get the current date (got it from this question)

var todayDate = kendo.toString(kendo.parseDate(new Date()), 'dd/MM/yyyy');
$("#initialDate").data("kendoDatePicker").value(todayDate);

So, how do I get the moth before with this? I tried to play with getMonth and setMonth, but couldn't make it work.

  • You basically have to obtain your desired date in JavaScript, see: https://stackoverflow.com/questions/26488043/javascript-get-previous-months-date – Dan Dumitru Jul 20 '17 at 15:14
  • @DanDumitru I saw a similar answer but I think I missed one point there. Was not using the getMonth currectly before, now it works fine :) Thank you. – Buttercup Cumbersnatch Jul 20 '17 at 15:24

1 Answers1

1

C#'s AddMonths alters the month of the DateTime much the sames as JavaScript's setMonth does. However, importantly, AddMonths takes into account edges cases like needing to go forward/backward a year or what happens when the particular date is not valid for that month. For example, if you subtract a month from March 29th, 2017 there was no February 29th that year. As a result, the date component also needs to change to 28. If it's a leap year, there will be, though, so you have to take that into account as well.

JavaScript's setMonth is relatively dumb by comparison. It literally just modifies the month and then lets the date overflow as needed. For example, with the previous scenario of subtracting a month from March 29th, 2017 JavaScript will actually give you March 1, 2017, as it went to February 1, and then added the date component of 29 days. Since February only has 28, it wraps back around to March. It is at least smart enough to take leap years into account, so in a year like 2016, you would actually end up with February 29th.

Regardless, JavaScript dates pretty much suck. If you truly want to work with them, I'd suggest using a library like Moment.js, which among other things, smartly handles these types of situations.

Your best solution, though, might be to simply persist the default value from C#, so you don't have to worry about having to calculate it later via JavaScript. For example, you could add a data-* attribute like:

.HtmlAttributes(new { data_default = DateTime.Now.AddMonths(-1) })

Then, you can simply read this value from the input and set the control to that when it's cleared.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • The February issue isn't really relevant in my case, since it goes to March 1st, that suits it's intent. I went for the solution that @DanDumitru linked, since I want the date to be calculated in a function, to be used everytime someone wants to clear the filters, it cleans them all. – Buttercup Cumbersnatch Jul 20 '17 at 15:56