2015-02-11 67 views
0
var Timeframe = React.createClass({ 
getInitialState: function() { 
    return { 
     tfs: getParameterByName('tfs'), 
     tfe: getParameterByName('tfe') 
    } 
}, 
formatTime: function(time) { 
    var full = new Date(time); 
    console.log(time); 
    console.log(full); 
    return (full).toString('dddd, MMMM ,yyyy'); 
}, 
render: function() { 
    return (
     <div>Timeframe: {this.formatTime(this.state.tfs)} - {this.formatTime(this.state.tfs)}</div> 
    ); 
} 

});反应无效日期

我有一个名为getParameterByName的外部js函数,在这种情况下,它将在unix时间返回一个时间。据我所知,Date()对这些数字非常适用,除了React之外,我从来没有遇到任何麻烦。当我在我的反应函数中尝试使用Date时,它将返回无效日期。有人知道为什么会发生这种情况吗?

+0

您是否验证过tfs和tfe设置正确?例如在不调用formatTime的情况下输出它们 – ryanbraganza 2015-02-11 18:32:26

+0

是的,这是工作,只是检查并在控制台中插入新的日期。工作罚款。 – 2015-02-11 18:34:38

+1

getParameterByName返回一个字符串而不是数字吗? – ryanbraganza 2015-02-11 18:39:44

回答

0

试试这个:full.toString('dddd, MMMM ,yyyy');

相反的:(full).toString('dddd, MMMM ,yyyy');

如果您需要日期工作,一个不错的选择就是使用Moment库。