I need to send a date value to the server in ISO Format "YYYY-MM-DDTHH:mm:ss.SSS[Z]"
I don't need the time details so I am setting them as zero.
For that I am using the below code
var today = new Date();
var todayWithTimeAsZero = new Date(today.getFullYear(), today.getMonth(), today.getDate(), 0, 0, 0);
I get todayWithTimeAsZero
as Tue Jul 25 2017 22:06:03 GMT+0530 (India Standard Time)
Now how do I convert this date into an ISO format. I researched everywhere but no luck.
I tried var day = todayWithTimeAsZero.toISOString();
but this creates a new date object with the time values populated like so 2017-07-24T18:30:00.000Z
. Also I have momentjs in my project may be in some way I can use that.
With moment.js
you can get the current date as UTC, then set the time values to zero and get the ISO string:
moment() // current date
.utc() // convert to UTC
.hours(0).minutes(0).seconds(0).milliseconds(0) // set time values to zero
.toISOString() // format to ISO8601
The value of the formatted string is 2017-07-25T00:00:00.000Z
.
You can also use the setUTCxxx
methods of Date
:
var today = new Date();
today.setUTCHours(0);
today.setUTCMinutes(0);
today.setUTCSeconds(0);
today.setUTCMilliseconds(0);
today.toISOString()
will be 2017-07-25T00:00:00.000Z
.