1

I am trying to increment the time by 1 hour for Timestamp in the format (YYYY-MM-DDTHH:MM:SS). This works perfectly fine till time is 22:00:00. However when input time is 23:00:00, the time component changes correctly to 00:00:00, however the corresponding date increments to over a month (Jumps 1 full month). Ex. 2020-07-31T23:00:00 changes to 2020-09-01T00:00:00. Any help on this is greatly appreciated.

var sdate = '2020-07-31T23:00:00';
var temp1 = sdate.split('T');
document.write(temp1);

var d = temp1[0].split('-');
var t = temp1[1].split(':');

document.write("<br />");
document.write(d);

document.write("<br />");
document.write(t);

var dt3 = new Date(d[0], d[1], d[2], t[0], t[1], t[2], 0);
var dt4 = dt3.setSeconds(dt3.getSeconds() + 3600);
var final = new Date(dt4);

document.write("<br />");
document.write(final);

document.write("<br />");
document.write(final.getFullYear());

document.write("<br />");
document.write(final.getMonth());

document.write("<br />");
document.write(final.getDate());

document.write("<br />");
document.write(final.getHours());

document.write("<br />");
document.write(final.getMinutes());

document.write("<br />");
document.write(final.getSeconds());
Barmar
  • 741,623
  • 53
  • 500
  • 612
Vijay
  • 11
  • 2
  • use the built-in [Date](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) object – Yousaf Jul 02 '20 at 07:54

3 Answers3

0

Why all of this complications? JS has an own Date Object

You can achieve to add 1 to the hour simply by:

var sdate = new Date('2020-07-31T23:00:00');
console.log(sdate.toISOString());

sdate.setHours(sdate.getHours()+1)
console.log(sdate.toISOString());
Greedo
  • 3,438
  • 1
  • 13
  • 28
  • Thank you so much - this works perfectly fine. Just a quick question - How do I get it back in the format of 'YYYY-MM-DDTHH:MM:SS' ? Currently it shows as Sat Aug 01 2020 00:00:00 – Vijay Jul 02 '20 at 07:59
  • `.toISOString()` should give you the format you want Also don't forget to mark the answer as accepted – Greedo Jul 02 '20 at 08:02
  • **Don't use the built–in parser**. Compare results in Safari vs other browsers, see [*JavaScript Date() inconsistence with Safari's and non-Safari implementation*](https://stackoverflow.com/questions/62683906/javascript-date-inconsistence-with-safaris-and-non-safari-implementation) and [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Jul 02 '20 at 08:10
  • I am not able to get it back into the format of YYYY-MM_DDTHH:MM:SS after adding 1 hour as you told above. Can you please help on this. .toISOString() is not working to get into this format. – Vijay Jul 02 '20 at 10:57
0

Your problem is that month you're thinking is july is really August because month number on javacript constructor are 0-indexed, so in this line:

var dt3= new Date(d[0], d[1], d[2], t[0], t[1], t[2], 0); 

you are setting 31 of Aug. For more information visit https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

Fran Aguilar
  • 110
  • 5
0

You are passing in 7 as the month to the constructor, but month counting starts at 0, so you think it's July, but it's already August! Time flies.

So that means when you add an hour, you go past midnight into September.

var sdate = '2020-07-31T23:00:00';
var temp1 = sdate.split('T');
document.write(`date I think I am starting with: ${sdate}`);

var d = temp1[0].split('-');
var t = temp1[1].split(':');

var dt3= new Date(d[0], d[1], d[2], t[0], t[1], t[2], 0); 
document.write("<br />");
document.write(`date I am actually starting with: ${dt3.toISOString()}`);

To get more consistent results, use a library like moment.js. Dates are hard, and you will always find corner cases.

Matt Ellen
  • 11,268
  • 4
  • 68
  • 90