简体   繁体   English

如何以YYYY-MM-DD格式在JS中获取第二天的日期?

[英]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.似乎是一个简单的问题,但是 JS 中的所有时区来龙去脉都让我很头疼。

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):我在 SO 上找到了以下代码片段来执行此操作(有点):

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?我已经通过无数 SO 帖子试图找到这个看似简单的问题的答案,但是对于任何给定的日期,无论用户所在的时区如何,我如何以 YYYY-MM-DD 格式获取第二天的日期? 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? ).格式为 YYYY-MM-DD 的字符串被解析为 UTC,因此在这种情况下,请使用 UTC 执行所有操作(请参阅为什么 Date.parse 给出不正确的结果?以及如何将 1 天添加到当前日期? )。

The toISOString method will return the string in the required format, just trim the redundant time part, eg toISOString方法将返回所需格式的字符串,只需修剪多余的时间部分,例如

 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?您是否尝试过 UTC 日期?

 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 ?正如@chrisbyte 所建议的那样,您是否尝试过使用toUTCString方法而不是toString()方法?

As a reminder , toString is the default used when you display the date object withim the console for example提醒一下,例如,当您在控制台中显示date对象时, toString是默认使用的

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)我认为您假设的“问题”只是对Date.toString()方法行为方式的不完整理解:此方法似乎返回表示Date对象的字符串,似乎使用此处提到的时区(在第一个示例中的评论中) )

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.最后,如果您想将日期字符串作为YYYY-MM-DD格式返回,您可能必须自己实现 :-/ ,或者使用toLocaleFormat方法,但它没有标准化。

The logic would be to add 24 hours in milliseconds to the current time.逻辑是将 24 小时以毫秒为单位添加到当前时间。 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. oneMoreDay 变量中增加了一天。 In your specific example you just wanted to add one more day to the ORIGINAL variable, so i'd do something such as:在您的具体示例中,您只想在 ORIGINAL 变量中再添加一天,因此我会执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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