【问题标题】:Moment JS Date format to remove minus sign from iso formatMoment JS 日期格式从 iso 格式中删除减号
【发布时间】:2017-05-24 13:24:15
【问题描述】:

我在我的应用程序中使用 moment.js 并且预期的日期格式是

2017-01-09T17:05:00.000 //Expected Result

好像我在哪里打电话

moment().format() 

我正在获取 ISO 8601 格式,即带有 T 和减号

(2017-01-14T17:05:00-06:00) // Actual result.

我应该用什么来获得这种带有 .000 的格式

【问题讨论】:

  • 如果你使用moment,只需指定你想要的格式。
  • 谢谢。我需要指定什么格式。
  • 您如何获得 ISO 日期? JSFiddle。适合我
  • 这里 OP 知道获取 ISO 8601 格式并寻找替代格式选项。

标签: momentjs


【解决方案1】:

ISO 8601 是 moment 的 format() API 的默认表示。但是您可以通过传递所需的格式模式来自定义它。

在你的情况下,moment().format('YYYY-MM-DD[T]HH:mm:ss.SSS') 会产生你正在寻找的东西

2017-01-10T11:55:56.621

在此处查找更多自定义选项:http://momentjs.com/docs/#/displaying/

--

另一个要知道的选项,虽然它仅适用于 UTC 时间:moment().toISOString()

2017-01-10T06:56:18.465Z

[来源:拉杰什]

【讨论】:

  • 谢谢兄弟。拯救了我的一天。
  • 这不是理想的方式。请查看我上一条评论中的参考链接
  • @Rajesh 您在 jsfiddle 中指的是 console.log(moment().toISOString()) 吗?
  • @JayKumar 我的意思是我将这个问题标记为重复的评论。该帖子地址正确,即,toISOString()
  • @Rajesh 有一个问题。来自官方文档:Note that .toISOString() always returns a timestamp in UTC, even if the moment in question is in local mode. This is done to provide consistency with the specification for native JavaScript Date .toISOString(), as outlined in the ES2015 specification.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-08
  • 2015-07-16
  • 1970-01-01
相关资源
最近更新 更多