什么是JavaScript的默认日期格式?

发布于 2025-02-13 12:02:19 字数 1307 浏览 0 评论 0原文

这种格式的名称是什么?

Wed Jul 06 2022 14:42:13 GMT-0400 (Eastern Daylight Time)

此格式是从new Date()输出“独立于输入格式”的默认格式。

如何使用Moment.js将日期变成该格式?

  • 示例:2022-07-15T00:00:00.000Z2015-03-25
console.log(moment(new Date()).toISOString());
// 2022-07-06T19:08:36.670Z

console.log(moment(new Date()).toString());
// Wed Jul 06 2022 15:08:36 GMT-0400

console.log(new Date());
// Wed Jul 06 2022 15:08:36 GMT-0400 (Eastern Daylight Time)

console.log(new Date().toString());
// Wed Jul 06 2022 15:08:36 GMT-0400 (Eastern Daylight Time)

您可以看到该moment(new Date())。moment(new Date())。toString() 不要与默认JS格式相同的格式输出。

  • moment(new Date())。toString()是接近的,但是仍然缺少(东部日光时间),是默认值的一部分。

What is the name of this format?

Wed Jul 06 2022 14:42:13 GMT-0400 (Eastern Daylight Time)

This format is the default format that is output "Independent of input format" from new Date().

How do you turn a date into that format using Moment.js?

  • Example: 2022-07-15T00:00:00.000Z or 2015-03-25
console.log(moment(new Date()).toISOString());
// 2022-07-06T19:08:36.670Z

console.log(moment(new Date()).toString());
// Wed Jul 06 2022 15:08:36 GMT-0400

console.log(new Date());
// Wed Jul 06 2022 15:08:36 GMT-0400 (Eastern Daylight Time)

console.log(new Date().toString());
// Wed Jul 06 2022 15:08:36 GMT-0400 (Eastern Daylight Time)

You can see that moment(new Date()).toISOString() and moment(new Date()).toString() DO NOT output that same format as the default JS format.

  • moment(new Date()).toString() is close, but is still missing the (Eastern Daylight Time) which is part of the default.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫腻 2025-02-20 12:02:19

这种格式的名称是什么?

它不是ISO,它是ECMA-262中指定的格式,在此处阅读更多: https://developer.mozilla.org/en-us/docs/web/javascript/reference/reference/global_objects/date/tostring

将其转换为ISO使用date> date.to.toisstring()代码>

BTW。不要使用w3schools.com本网站有重大错误

如何使用Moment.js将日期变成该格式?

  1. ISO:MOMM(new Date())。toisostring()
  2. 2015-03-25moment(new Date())。格式('yyyy-mm -dd')
  3. ecma-262:moment(new Date())。todate()。toString()

ps ps我将考虑使用其他库作为MONM.JS。更新了,但是如果您别无选择,就可以了。

What is the name of this format?

It's not ISO, it's a format specified in ECMA-262, read more here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toString

To convert it to ISO use date.toISOString()

Btw. don't use w3schools.com this website has major errors

How do you turn a date into that format using Moment.js?

  1. ISO: moment(new Date()).toISOString()
  2. 2015-03-25: moment(new Date()).format('yyyy-MM-dd')
  3. ECMA-262: moment(new Date()).toDate().toString()

P.S. I would consider using a different library as moment.js isn't getting updates anymore, but if you don't have a choice it's fine.

2025-02-20 12:02:19

答案:

console.log(Moment(new Date())。todate());

,它比应该更难找到。

console.log(moment(new Date()).toDate());
// Wed Jul 06 2022 15:25:52 GMT-0400 (Eastern Daylight Time)

console.log(new Date());
// Wed Jul 06 2022 15:25:52 GMT-0400 (Eastern Daylight Time)

console.log(new Date().toString());
// Wed Jul 06 2022 15:25:52 GMT-0400 (Eastern Daylight Time)


奖励:

console.log(moment(1234, moment.ISO_8601, true).isValid());
// true

console.log(1234 instanceof Date)
// false

console.log(new Date() instanceof Date);
// true

Answer:

console.log(moment(new Date()).toDate());

That was way harder to find than it should have been.

console.log(moment(new Date()).toDate());
// Wed Jul 06 2022 15:25:52 GMT-0400 (Eastern Daylight Time)

console.log(new Date());
// Wed Jul 06 2022 15:25:52 GMT-0400 (Eastern Daylight Time)

console.log(new Date().toString());
// Wed Jul 06 2022 15:25:52 GMT-0400 (Eastern Daylight Time)


Bonus:

console.log(moment(1234, moment.ISO_8601, true).isValid());
// true

console.log(1234 instanceof Date)
// false

console.log(new Date() instanceof Date);
// true
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文