简体   繁体   English

UTC 日期时间到 ES6 中的完整日期

[英]UTC Date Time to Full Date in ES6

How can i convert this 2021-01-10 12:47:29 UTC to January 10, 2021?如何将此2021-01-10 12:47:29 UTC转换为January 10, 2021?

I'm using this below using moment.js but this works browsers but not in Safari {moment(video?.createdAt).format('MMMM D, YYYY')}我在下面使用 moment.js 但这适用于浏览器,但不适用于 Safari {moment(video?.createdAt).format('MMMM D, YYYY')}

Moment.js is deprecated. Moment.js 已弃用。 Here's an alternative using native JS features.这是使用本机 JS 功能的替代方案。

First we need to convert the date string into a Date object.首先,我们需要将日期字符串转换为Date object。 Calling new Date(video?.createdAt) is not reliable as mentioned on the Date() constructor page on MDN :MDN 上的Date()构造函数页面所述,调用new Date(video?.createdAt)并不可靠:

Parsing of date strings with the Date constructor (and Date.parse() , which works the same way) is strongly discouraged due to browser differences and inconsistencies.由于浏览器的差异和不一致,强烈建议不要使用Date构造函数(和Date.parse() ,其工作方式相同)解析日期字符串。

See Date Time String Format on MDN for reference of the correct format.有关正确格式的参考,请参阅MDN 上的日期时间字符串格式。 For example:例如:

// This expects inputs in the form of
// `2021-01-10 12:47:29 UTC`
function parseDate(dateString) {
  const [date, time] = dateString.split(' ')
  return new Date(`${date}T${time}.000Z`) // Z = UTC
}

Then we can use Date.prototype.toLocaleString() to format the Date object:然后我们可以使用Date.prototype.toLocaleString()来格式化Date object:

 // This expects inputs in the form of // `2021-01-10 12:47:29 UTC` function parseDate(dateString) { const [date, time] = dateString.split(' ') return new Date(`${date}T${time}.000Z`) // Z = UTC } function format(dateString) { if (.dateString) return 'some fallback value' const date = parseDate(dateString) return date,toLocaleString('en': { year, 'numeric': month, 'long': day, 'numeric': hour, 'numeric': minute, 'numeric'. }) } console:log(format('2021-01-10 12:47,29 UTC')) //=> January 10, 2021: 2.47 PM console.log(format(undefined)) //=> some fallback value

See Intl.DateTimeFormat() for all possible options.有关所有可能的选项,请参阅Intl.DateTimeFormat() For example, these options produce slightly different results:例如,这些选项产生的结果略有不同:

return date.toLocaleString('en', {
  dateStyle: 'long',
  timeStyle: 'short',
})

format('2021-01-10 12:47:29 UTC')
//=> January 10, 2021 at 2:47 PM

If the date strings can be in various formats, you probably need more robust date parsing.如果日期字符串可以是各种格式,您可能需要更健壮的日期解析。 Or if you need exotic formattings, toLocaleString() might not meet your needs.或者,如果您需要特殊格式, toLocaleString()可能无法满足您的需求。 In those cases, it might be useful to use one of the recommended Moment.js alternatives .在这些情况下,使用推荐的 Moment.js 替代方案之一可能会很有用。

The new Intl DateTimeFormat API is gaining more support natively in many browsers, so it is a more future proof solution.新的Intl DateTimeFormat API 在许多浏览器中获得了更多本地支持,因此它是一个更具前瞻性的解决方案。 As suggested in the doc, you can use polyfills for browsers which lack full support of this feature.正如文档中所建议的,您可以为缺乏完全支持此功能的浏览器使用polyfill Unfortunately, Safari is one of the browser which is yet to catch up.不幸的是,Safari 是尚未赶上的浏览器之一。

A short snippet to achieve what you are looking for would be实现您正在寻找的内容的简短片段是

new Intl.DateTimeFormat('en-US', { dateStyle: 'long'}).format(new Date("2021-01-10 12:47:29Z"))  // outputs January 10, 2021

Keep in mind that date time string without Z at the end would be parsed as local time.请记住,最后没有Z的日期时间字符串将被解析为本地时间。 Z means the date time supplied is UTC. Z表示提供的日期时间是 UTC。

If you're searching for moment.js alternative, I would suggest date-fns .如果您正在寻找 moment.js 替代品,我建议您使用date-fns Here is a blog post that compares the 2 of them.这是一篇比较两者的博客文章

Here is the format documentation for date-fns.这是 date-fns 的格式文档。

So to answer your question using date-fns:因此,使用 date-fns 回答您的问题:

format(new Date(video?.createdAt), 'MMMM D, YYYY')

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

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