我从 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
最佳答案
如果你pass an Object to the Moment constructor ,该对象应该具有名为 year
、month
等的字段。您的对象没有,因此 Moment 无法解析它并判定它无效。
正如问题所附评论中所述,尝试仅使用日期字符串创建 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();
关于javascript - momentJS to Date() 返回无效日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42305575/