momentJS的TODATE()返回无效日期
问题描述:
我收到在以下格式从PHP:momentJS的TODATE()返回无效日期
"end_date":{"date":"2017-02-17 18:52:31.000000","timezone_type":3,"timezone":"UTC"}}]
在JS/AngularJS结束我做如下:
var end_date = Lease.period_ferme[idx].end_date
$scope.frame[idx].end_date = moment(end_date).toDate()
console.log('After');
console.log($scope.frame[idx].end_date); //invalid date
答
如果你pass an Object to the Moment constructor,该对象应有名为year
,month
等的字段。您的对象没有,所以Moment无法解析它并确定它无效。
如连接到你的问题的评论中提及,尽量只用日期字符串创建一个瞬间对象:
$scope.frame[idx].end_date = moment(end_date.date).toDate();
,或者因为你的JSON指定UTC,尝试创建使用Moment.utc
一个瞬间的对象:
$scope.frame[idx].end_date = moment.utc(end_date.date).toDate();
时刻( “2017年2月17日18:52:31.000000”)。TODATE() 星期五2017年2月17日18点52分31秒GMT-0500(美国东部标准时间)看起来科尔等我。 – inoabrian
为什么这个问题被删除?不知道'momentJs'的Somone说我使用了错误的方法? – Volatil3
你是否将整个对象传递给moment()作为参数?也许我不明白,但是你不应该使用'moment(end_date.date)'? –