【问题标题】:converting epoch into month将纪元转换为月份
【发布时间】:2015-03-17 10:59:55
【问题描述】:

我想将纪元“1326067200000”转换为月份,即“一月”。

var d = new Date(1326067200000).format("%B");
print (d);

但不工作。

最简单、最优雅的方法是什么?

【问题讨论】:

  • 只有toLocaleFormat() 可以做你想做的事,但几乎没有支持。另一个选项只是 getMonth() 和一个开关 ...

标签: javascript datetime epoch


【解决方案1】:

toLocaleDateString():

 var month = new Date(1326067200000).toLocaleDateString('en-US', {month: 'short'})

在撰写本文时,您必须修补 Safari:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString#Browser_compatibility

或者,您可以使用 getMonth() 获得编号为 0 的月份:

var monthNames = ['Jan','Feb','Mar']
var month = monthNames[new Date(1326067200000).getMonth()];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-14
    • 1970-01-01
    • 2015-09-07
    • 1970-01-01
    • 2016-10-18
    • 2011-08-17
    • 1970-01-01
    • 2021-07-20
    相关资源
    最近更新 更多