简体   繁体   中英

How do I get the next day's date in JS in YYYY-MM-DD format?

Seems like a simple question, but all the timezone ins and outs in JS are causing me a bunch of headaches.

Basically, if I have a date like the following:

2018-04-06

I want to be able to get the next day's date as such:

2018-04-07

I found the following snippet on SO for doing this (kind of):

var date = new Date('2018-04-06');
date.setDate(date + 1);

The problem is that I'm getting the date back with the adjusted timezone, and because I'm in the US ET timezone, it's giving me that date minus five hours, which is actually the same day as where I started.

I've been through countless SO posts trying to find an answer to this seemingly simple question, but for any given date, regardless of the timezone the user is in, how do I get the next day's date in YYYY-MM-DD format? Thank you.

Strings in the format YYYY-MM-DD are parsed as UTC so in this case, do everything in UTC (see Why does Date.parse give incorrect results? and How can I add 1 day to current date? ).

The toISOString method will return the string in the required format, just trim the redundant time part, eg

 let s = '2018-04-06'; let d = new Date(s); d.setUTCDate(d.getUTCDate() + 1); console.log(d.toISOString().substr(0,10));

Did you try with the UTC date?

 var date = new Date('2018-04-06'); console.log(date.toUTCString()); date.setDate(date.getDate() + 1); console.log(date.toUTCString());

As it was suggested by @chrisbyte, have your tried to use toUTCString method instead of toString() method ?

As a reminder , toString is the default used when you display the date object withim the console for example

I think the "problem" you're assuming is just an incomplete understanding how Date.toString() method behaves: this method seems to to return string representing a Date object but seems to use timezone as mentionned here (on the comment in 1st example)

Here my snippet to understand more:

 const originalDate = new Date('2018-04-06'); // retrieving the original timestamp const originalTimestamp = originalDate.valueOf() // displaying the original date (non UTC / UTC) console.log(`original date (timezone dependent): ${originalDate.toString()}`) console.log(`original date (timezone independent): ${originalDate.toUTCString()}`) // we add one more day originalDate.setDate(originalDate.getDate() +1) const dayAfterTimestamp = originalDate.valueOf() // displaying the original date (non UTC / UTC) console.log(`updated date (timezone dependent): ${originalDate.toString()}`) console.log(`updated date (timezone independent): ${originalDate.toUTCString()}`) // check the differences (in milliseconds) console.log(`difference: ${(dayAfterTimestamp-originalTimestamp)}`) // displaying the original format (timezone independent)

At last if you want to return the date string as a YYYY-MM-DD format you may have to implement it yourself :-/ , or use toLocaleFormat method but it isn't standardized.

The logic would be to add 24 hours in milliseconds to the current time. As an example:

var myDate = new Date();
var oneMoreDay = new Date();
oneMoreDay.setTime(myDate.getTime() + 86400000);
console.log(myDate.getDate());
console.log(oneMoreDay.getDate());

An additional day has been added to the oneMoreDay variable. In your specific example you just wanted to add one more day to the ORIGINAL variable, so i'd do something such as:

date.setTime(date.getTime() + 86400000);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM