新的日期(epoch)在Ember组件内返回无效日期

Bra*_*cil 2 javascript ember.js

我有一个date-filter我在Ember应用程序中使用的组件,它只适用于初始渲染,而不是页面重新加载,或者即使我保存文件(触发应用程序进行实时更新).

在我的应用程序的主模板中,我像这样渲染日期过滤器,传递一个unix时间戳

{{date-filter unixepoch=item.date}}
Run Code Online (Sandbox Code Playgroud)

然后,在 components/date-filter.js,我使用一个被调用的计算属性timeConverter来将unix epoch更改为根据用户选择的语言格式化的时间字符串,然后在我的templates/components/date-filter.hbs文件中我{{timeConverter}}显示结果

timeConverter: function(){
    //step 1: get the epoch I passed in to the component
    var epoch = this.get('unixepoch');
    //step 2: create a human readable date string such as `Jun 29, 2015, 12:36PM`
    var datestring = new Date(epoch)

    //do language formatting --code omitted as the problem is with step2

}
Run Code Online (Sandbox Code Playgroud)

如果我刷新页面甚至保存文件step 2,那就是失败(返回invalid date).它始终在第一次调用此组件时返回正确的日期字符串.即使我new Date(epoch)在父组件中,并尝试将结果传递给此组件(进行外语格式化),我遇到了同样的问题.

问题:我怎样才能弄清楚新日期(纪元)内发生了什么,或者它是否与组件有关?

T.J*_*der 6

我怀疑你的epoch值是一个字符串(所有数字).如果是的话,那么

var datestring = new Date(+epoch);
// Note ------------------^
Run Code Online (Sandbox Code Playgroud)

......会解决它.请注意,JavaScript使用较新的"自The Epoch以来的毫秒"而不是较旧的(原始)"自Epoch以来的秒数".因此,如果这样做会开始给你提供日期,但是它们会比你期望的更早回来,你可能会想要epoch * 1000.

如果它是一个不是所有数字的字符串,它根本不是一个纪元值.该规范所要求的字符串值new Date,以了解是一个在ECMAScript5添加固定在ECMAScript6(关于当没有时区指标做什么).