vineet
vineet

Reputation: 14246

momentjs toDate() date return not same as format() date

I am using moment 2.16.0 and want starting days of month. There are different result of toDate() and format() method. Here is jsfiddle.

code:-

var time=moment().subtract(0,'months').startOf("month").format();
console.log(time); //2016-12-01T00:00:00+05:30

var time2=moment().subtract(0, 'months').endOf("month").format();
console.log(time2); //2016-12-31T23:59:59+05:30

var time=moment().subtract(0,'months').startOf("month").toISOString();
console.log(time); //2016-11-30T18:30:00.000Z  here i want somethings like 2016-12-01T00:00:00.000Z


var time2=moment().subtract(0, 'months').endOf("month").toISOString();
console.log(time2); // 2016-12-31T18:29:59.999Z here i want somethings like 2016-12-31T59:59:59.000Z

Upvotes: 0

Views: 2409

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074909

All of your operations are using moment with local time except the toISOString, which will give you the string in UTC. Since your timezone is offset from UTC, naturally the local time string (from format) and the UTC time string (from toISOString) are very different.

here i want somethings like 2016-12-01T00:00:00.000Z

That would be a different time from what that Moment instance represents.

If you want something in an ISO-8601 format but in local time, you can use format with the appropriate set of formatting tokens, but you don't wan the Z at the end because, again, you're not dealing with UTC ("Zulu") time, you're dealing with local time.

moment().format("YYYY-MM-DDThh:mm:ss.SSS")

Upvotes: 3

Related Questions