如何在使用 new Date() 时保留原始输入日期格式?

huangapple go评论79阅读模式
英文:

JavaScript: How to keep the original input date format when using new Date()?

问题

我有一个日期,格式如下:"2023-05-19T18:30:00.000Z"
我的代码如下:

date(value) {
  const myDate = new Date("2023-05-19T18:30:00.000Z")
}

上述代码返回的日期是

 "Sat May 20 2023 00:00:00 GMT+0530 (印度标准时间)"

问题是,我的输入日期是5月19日,但在使用new Date后,它变成了5月20日。如何阻止日期和时间发生变化?

英文:

I have a date which is in following format "2023-05-19T18:30:00.000Z"
My code is as follows:

date(value) {
const myDate = new Date("2023-05-19T18:30:00.000Z")
}

Above code is giving date as

 "Sat May 20 2023 00:00:00 GMT+0530 (Indian Standard Time)".

Problem is my input date is 19 May but after doing
new Date it is giving May 20. how can I stop date and time getting changed?

答案1

得分: 1

console.log(new Date("2023-05-19T18:30:00.000Z").toString())

// 你在这里使用了 [日期时间字符串格式](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#date_time_string_format) 来初始化 `Date` 对象,其中结尾的 `Z` 表示你设置了零 [UTC 偏移](https://en.wikipedia.org/wiki/UTC_offset),因此它表示 [UTC+00:00](https://en.wikipedia.org/wiki/UTC%2B00:00) 时区。*(感谢 @RobG 的澄清。)*

// 但是,当你将 `Date` 对象转换回字符串时,它会默认为用户的本地时区,并相应地转换日期和时间,这就是为什么它们看起来不同(除非你的本地时区恰好是 UTC+00:00)。

// 要获得与 UTC+00:00 明确对应的字符串,请使用 [toUTCString()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toUTCString) 方法:

console.log(new Date("2023-05-19T18:30:00.000Z").toUTCString())

// 你也可以获取任何 `Date` 组件作为数字:

let date = new Date("2023-05-19T18:30:00.000Z");
console.log(
    `Year: ${date.getUTCFullYear()},`,
    `Month: ${date.getUTCMonth() + 1},`, // 注意:月份是从零开始的,所以必须加1!
    `Day: ${date.getUTCDate()},`,
    `Hours: ${date.getUTCHours()},`,
    `Minutes: ${date.getUTCMinutes()},`,
    `Seconds: ${date.getUTCSeconds()},`,
    `Milliseconds: ${date.getUTCMilliseconds()}`
);
英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

console.log(new Date(&quot;2023-05-19T18:30:00.000Z&quot;).toString())

<!-- end snippet -->

Here you are using Date time string format to initialize Date object, where trailing Z means you're setting it with zero UTC offset, so that it represents UTC+00:00 timezone. (Thanks to @RobG for clarification.)

However, when you're converting your Date object back into a string, it defaults to a local time zone of the user and translates date and time accordingly, which is why you're seeing them different (unless your local time zone is exactly UTC+00:00).

To obtain a string in explicit correspondence with UTC+00:00, use toUTCString() method:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

console.log(new Date(&quot;2023-05-19T18:30:00.000Z&quot;).toUTCString())

<!-- end snippet -->

Also you can get any of the Date components as a number:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let date = new Date(&quot;2023-05-19T18:30:00.000Z&quot;);
console.log(
    `Year: ${date.getUTCFullYear()},`,
    `Month: ${date.getUTCMonth() + 1},`, // note: month is zero-based, so has to be incremented!
    `Day: ${date.getUTCDate()},`,
    `Hours: ${date.getUTCHours()},`,
    `Minutes: ${date.getUTCMinutes()},`,
    `Seconds: ${date.getUTCSeconds()},`,
    `Milliseconds: ${date.getUTCMilliseconds()}`
);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月5日 02:08:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76401803.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定