【问题标题】:Javascript how to convert epoch into local dateJavascript如何将纪元转换为本地日期
【发布时间】:2016-01-21 04:58:29
【问题描述】:

我正在将纪元日期转换为本地日期:

$("time").each(function() {
    var date = $(this).text(); // gives me "1325419200000"
    newDate = new Date(date); // gives me Invalid Date
});

html:

<td class="date">
    <time datetime="{{date}}">{{date}}</time>
</td>

如何将纪元日期转换为本地日期,格式为:'MMM DD, YYYY h:mm:ss A'。我怎样才能做到这一点?

【问题讨论】:

  • 你需要newDate = new Date(parseInt(date));
  • 使用像moment.js这样的库。它会为你省去很多挫折。
  • 我希望已经回答了这个问题。看到这个链接stackoverflow.com/questions/4631928/…

标签: javascript jquery


【解决方案1】:

DateObject 不接受字符串纪元。所以你需要把它转换成整数。

Check this out

var epoch = "1325419200000"
var date = new Date(epoch);
// return Invalid Date

date = new Date(parseInt(epoch));
//return Sun Jan 01 2012 21:00:00 GMT+0900

【讨论】:

    猜你喜欢
    • 2011-06-05
    • 2012-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-19
    • 2018-04-24
    • 2011-12-06
    • 2012-11-22
    相关资源
    最近更新 更多