For some project tasks, I need to reduce the date entered by the user by three hours to get GMT.

I do it like this:

var GMTTimeShift=3; var fdate=document.getElementById("fd1").value.split('-'); var d1=new Date(); d1.setFullYear( fdate[2],parseInt(fdate[1])-1,fdate[0] ); d1.setHours(parseInt(document.getElementById("ft1").value), parseInt(document.getElementById("ft2").value)); parseInt(document.getElementById("ft2").value)); var a=d1.getTime()-GMTTimeShift*3600000; d1.setTime(a); console.log(d1); 

Accordingly, there are three input fields in which the time is entered: the hour of the minute is the date with hyphens, for example
00 00 01-08-2012
So here.
When going from month to month, namely, on August 1 and September 1, I catch a strange glitch - we do not get 21:00 07-30-07, but 21 00 30-11-2011 21:00
in the remaining months, the correction is true. Tell me, please, the reason for this behavior.

  • Try to take time like this. If there is the same problem, then the problem is not in this part of the code. datestr = document.getElementById ("ft1"). value + ":" + document.getElementById ("ft2"). value + "" + document.getElementById ("fd1"). value; d1 = Date.parse (datestr); d1.setTime (d1.getTime () - GMTTimeShift * 3,600,000); - ReinRaus

1 answer 1

Thanks to the forum, the enemy has been detected and neutralized!
namely, in this place:

 d1.setFullYear( fdate[2],parseInt(fdate[1])-1,fdate[0] ); 

The parseInt () function has a second, optional parameter that is responsible for the number system when translating. Without his instructions, the function itself decides what the base of the number system is, and of course, it does not guess, with the result that such a byaka is obtained.
The correct code in this particular case is:

 d1.setFullYear( fdate[2],parseInt(fdate[1],10)-1,fdate[0] ); 

parseInt () - it is so insidious! .. :)