Volatil3
Volatil3

Reputation: 14988

momentJS's toDate() returns invalid Date

I am getting in following format from PHP:

"end_date":{"date":"2017-02-17 18:52:31.000000","timezone_type":3,"timezone":"UTC"}}]

at JS/AngularJS end I am doing following:

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

Upvotes: 0

Views: 615

Answers (1)

Dan O
Dan O

Reputation: 6090

if you pass an Object to the Moment constructor, that object should have fields named year, month, etc. Your object does not, so Moment can't parse it and decides it's invalid.

As mentioned in the comment attached to your question, try creating a Moment object with just the date string:

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

or, since your JSON specifies UTC, try creating a Moment object using Moment.utc:

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

Upvotes: 2

Related Questions