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

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

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.

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.

Share Improve this question asked Feb 18, 2020 at 21:07 HartleySanHartleySan 7,84018 gold badges74 silver badges136 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 5

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, e.g.

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 inplete 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 ment 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);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745113427a4611982.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信