momentJS的toDate()返回无效的Date

时间:2017-02-17 19:04:00

标签: javascript momentjs

我从PHP获得以下格式:

"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

1 个答案:

答案 0 :(得分:1)

如果您pass an Object to the Moment constructor,该对象应包含名为yearmonth等的字段。您的对象不会,因此Moment无法对其进行解析并将其确定为&#39 ; s无效。

正如您的问题附带的评论中所述,尝试使用日期字符串创建一个Moment对象:

$scope.frame[idx].end_date = moment(end_date.date).toDate();

或者,由于您的JSON指定了UTC,请尝试使用Moment.utc创建一个Moment对象:

$scope.frame[idx].end_date = moment.utc(end_date.date).toDate();