-3

I am facing very strange behavior of setMonth function of JS. On click of a image i am showing a calendar, having all days of current month and some next and previous month's date (in Grey color).

Whenever current date selected is 31st date of any month. suppose 31 may 2017 I select i will set in a textfield. If I click on 2 june 2017 rather then setting textfield to 2 june 2017 it sets 2 July 2017 ?

please suggest whats wrong is going here.

Code snippet used as follows

var tempDate = new Date(current); //Suppose : current --> Wed May 31 16:09:00 UTC+0530 2017 
var dayOfMonth = parseInt(element.text(), 10); //Suppose : element.text() --> 2   
tempDate.setMonth(tempDate.getMonth() + (dayOfMonth > 15 ? -1 : 1)); //tempDate.getMonth() + (dayOfMonth > 15 ? -1 : 1) returns 10
tempDate.setDate(dayOfMonth);

//Output Expected : 02 June 2017 but it gives 02 July 2017

sebenalern
  • 2,515
  • 3
  • 26
  • 36
user222419
  • 11
  • 5
  • 3
    Not really clear what you're asking, but months are 0 based. – Evan Trimboli Jun 03 '16 at 11:44
  • Share minimalistic sample replicating your issue. And ya, unclear what you are asking regarding the 31st day. `Tue Oct 31` **&** `Output Expected : 02 June 2017 but it gives 02 July 2017` **&** `suppose 31 may 2017 I select i will set in a textfield. If I click on 2 june 2017 rather then setting textfield to 2 june 2017 it sets 3 July 2017`. And your posted picture just suppose you are setting wrong datepicker date format but completly unrelevant to what all your body question is talking about – A. Wolff Jun 03 '16 at 11:55
  • yeah updated the question. Actually made mistake while setting thedate for example. – user222419 Jun 03 '16 at 14:09
  • This question makes no sense. Please see [*How to create a minimal, complete and verifiable example*](http://stackoverflow.com/help/mcve). – RobG Jun 05 '16 at 06:07

2 Answers2

1

In javascript date object, Month is starting form 0, so if you want to generate any month, then do accordingly. Refer this url for Date: MDN Date

As your code , I think you made mistake at dayOfMonth > 15 ? 1 : -1

var dayOfMonth  = 13;
alert((dayOfMonth > 15 ? 1 : -1))
var tempDate = new Date(); 
tempDate.setMonth(tempDate.getMonth() + (dayOfMonth > 15 ? 1 : -1)); 
tempDate.setDate(dayOfMonth);
alert(tempDate)
RobG
  • 142,382
  • 31
  • 172
  • 209
bharatpatel
  • 1,203
  • 11
  • 22
  • 1
    Ok but `getMonth()` is zero based too and OP is using it to set the month. So i'm not sure it is the issue here – A. Wolff Jun 03 '16 at 12:00
  • 1
    Thanks for quick response. I found the fix. Actually JavaScript's Date objects allow you to give invalid combinations of months and days; they automatically correct those for you (so for instance, if you set the day of the month to 31 when the month is June, it automatically makes it July 1st). – user222419 Jun 03 '16 at 14:06
  • @user222419—that depends on what you call "invalid". The Date object's methods are all well defined in the standard and their behaviour is very convenient for doing Date manipulation. Would you expect 30 June + 1 day to return an invalid date or 1 July? – RobG Jun 03 '16 at 14:20
  • Thanks for Quick response guys. I found the solution, I had to debug the code so many time to understand the issue. Actually I came across that I should setDate before setMonth. I change the sequence and It worked. :-) – user222419 Jun 03 '16 at 14:23
  • 1
    Please do not reference w3schools, the site is full of errors. Use ECMA-262 or MDN. – RobG Jun 05 '16 at 06:02
0

I think that is for the standard behavior of the operation when the month does not have the day (31) the operation take the next month that have that day

>var date = new Date('2018/10/31');
undefined
>date
Wed Oct 31 2018 00:00:00 GMT-0600 (hora estándar central)
>date.setMonth(date.getMonth() + 1);
1543644000000
>date
Sat Dec 01 2018 00:00:00 GMT-0600 (hora estándar central)
Johan.CR
  • 25
  • 3