【问题标题】:javascript: What is the shortest way to convert Unix to long date formatjavascript:将Unix转换为长日期格式的最短方法是什么
【发布时间】:2018-05-14 12:22:01
【问题描述】:

我有一个 ISO 时间字符串:

"2018-05-14T14:04:53.16"

我需要将其转换为以下内容:

"May 05, 2018"

我知道的方法是先使用解析将其转换为时间戳,然后再转换为新日期:

let timestamp = new Date(Date.parse("2018-05-14T14:04:53.16"))

然后分别获取每个部分,将它们映射到映射数组,然后将它们连接起来:

let monthNames = ['January','Fabruary'...];
let month = timestamp.getMonth(); //gatDay/getYear
let monthName = monthNames[month - 1] 

然后最后将所有部分连接成一个字符串:

let finalString = monthName+' '+day+', '+year;

有没有更短的方法来做到这一点? 我问是因为这两种日期格式都被 javascript Date 对象识别,但我找不到在两者之间转换的捷径。

【问题讨论】:

  • 2018-05-14 是 5 月 14 日,而不是 5 月 5 日。"2018-05-14T14:04:53.16" 不是 unix 时间戳。在这种情况下建议使用 MomentJS。
  • new Date(Date.parse("2018-05-14T14:04:53.16"))new Date("2018-05-14T14:04:53.16") 相同。至少有一个正在使用的浏览器无法正确解析 2018-05-14T14:04:53.16。

标签: javascript ecmascript-6 ecmascript-5


【解决方案1】:

您可以使用toString 和一些字符串操作将timestamp 转换为所需的格式:

timestamp.toString().replace(/\w+ (\w+ \d+)( \d+).*/, "$1,$2")

【讨论】:

  • 不错!非常感谢!这是一个有角的管道......没有想到正则表达式选项。
  • 时间戳已经是字符串了,想必你的意思是new Date(timestamp).toString().replace(...)Date.prototype.toString 的输出取决于实现,可能会或可能不会返回预期的值。
  • @RobG,我不确定你对第一点的意思。我盯着这个问题,我只能看到timestamp 变量是Date 实例,而不是字符串。您对Date.prototype.toString 的输出是正确的,但实际上有一个共识:输出是always in the American English representation,在FF、Chrome、IE11 和Edge 中完全相同。这反映了 EcmaScript2019 草案,它不再被定义为特定于实现。
  • 我可能对变量名“timestamp”读了太多,OP以“2018-05-14T14:04:53.16”开头,这是一个时间戳。所有使用中的实现都符合当前草案还需要很长时间,许多设备的升级能力有限并且运行旧版本。 OP的要求实在是构思不周,目标应该是写出兼容、可靠、可维护的代码,而不是尽可能短。
  • @RobG,你知道任何不产生这种输出格式的示例 JS 引擎吗?注意:我当然同意你最后提到的目标。
【解决方案2】:

你也可以使用 moment.js:

var newDate = new moment("2018-05-14T14:04:53.16");
var html = 'Result: <br/>';
html += 'Formatted: ' + newDate.format('MMM DD, YYYY');

$('#output').html(html);

JSFiddle:https://jsfiddle.net/okd4mdcw/

【讨论】:

  • 我不认为我会为此添加另一个库
  • 这是有道理的@TKdev,你不想为这个特定问题引入moment.js。不过,这是一个很棒的图书馆,请记住!
【解决方案3】:

替代(?)

console.log(
new Date("2018-05-14T14:04:53.16").toUTCString().substr(0,12)
)

性能测试!(使用benchmark.js

var suite = new Benchmark.Suite;

// add tests
suite.add('toUTCString().substr', function() {
  new Date("2018-05-14T14:04:53.16").toUTCString().substr(0,12)
})
.add('Regex', function() {
  timestamp = new Date(Date.parse("2018-05-14T14:04:53.16"))
  timestamp.toString().replace(/\w+ (\w+ \d+)( \d+).*/, "$1,$2")
})
.add('toUTCString().split', function() {
  var d = new Date("2018-05-14T14:04:53.16").toUTCString().split(" ");
    d[2] +  ", " + d[1] + " " +d[3]
})
// add listeners
.on('cycle', function(event) {
  console.log(String(event.target));
})
.on('complete', function() {
  console.log('Fastest is ' + this.filter('fastest').map('name'));
})
// run async
.run({ 'async': true });
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/platform/1.3.5/platform.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/2.1.4/benchmark.min.js"></script>

更新,输出错误。一种方式,使用 split 重构:

var d = new Date("2018-05-14T14:04:53.16").toUTCString().split(" ")

console.log(
d[2] +  " " + d[1] + ", " +d[3]
)

【讨论】:

  • 但这会输出“Mon, 14 May”,而不是“May 14, 2018”
  • toUTCString 的输出取决于实现,因此可能不会返回所需的格式。它还将时区从本地转换为 UTC。
  • toLocaleString()
  • 但是,不要对页面上的每个人都投反对票,而是向我们展示您可以做得更好ô 大师。或者,你应该学会忽略,Rob,我看到你经常这样做。
  • @Cryptopat — 它是许多 similar questions 的副本。我不明白为什么人们继续回答它而不是将其标记为重复。如果我能让人们更多地思考他们的答案,他们也许会有一些好处。或不。 :-/
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-31
  • 1970-01-01
  • 1970-01-01
  • 2014-05-30
  • 1970-01-01
  • 2023-03-15
相关资源
最近更新 更多