How to Output Date in JavaScript in Iso 8601 Without Milliseconds and With Z

Stripping milliseconds from Extended ISO format

Since ISO date format is fixed width up until the millisecond portion, an alternative to splitting on the '.' is to simply use substring, then replace the "Z" timezone designator:

var d = new Date()

d.toISOString().substring(0,19)+'Z'

"2015-07-01T21:27:45Z"

How do I format a date as ISO 8601 in moment.js?

moment().toISOString(); // or format() - see below

http://momentjs.com/docs/#/displaying/as-iso-string/

Update
Based on the answer: by @sennet and the comment by @dvlsg (see Fiddle) it should be noted that there is a difference between format and toISOString. Both are correct but the underlying process differs. toISOString converts to a Date object, sets to UTC then uses the native Date prototype function to output ISO8601 in UTC with milliseconds (YYYY-MM-DD[T]HH:mm:ss.SSS[Z]). On the other hand, format uses the default format (YYYY-MM-DDTHH:mm:ssZ) without milliseconds and maintains the timezone offset.

I've opened an issue as I think it can lead to unexpected results.

How do I output an ISO 8601 formatted string in JavaScript?

There is already a function called toISOString():

var date = new Date();
date.toISOString(); //"2011-12-19T15:28:46.493Z"

If, somehow, you're on a browser that doesn't support it, I've got you covered:

if (!Date.prototype.toISOString) {
(function() {

function pad(number) {
var r = String(number);
if (r.length === 1) {
r = '0' + r;
}
return r;
}

Date.prototype.toISOString = function() {
return this.getUTCFullYear() +
'-' + pad(this.getUTCMonth() + 1) +
'-' + pad(this.getUTCDate()) +
'T' + pad(this.getUTCHours()) +
':' + pad(this.getUTCMinutes()) +
':' + pad(this.getUTCSeconds()) +
'.' + String((this.getUTCMilliseconds() / 1000).toFixed(3)).slice(2, 5) +
'Z';
};

}());
}

console.log(new Date().toISOString())

Javascript date format like ISO but local

AFAIK you can't format dates in javascript (without using external libraries). The best you could do is "format it yourself". I mean:

var date = new Date();
var year = date.getFullYear();
var month = date......


var ISOdate = year + "-" + month + "-" + .... ;

But there are some good libraries that will let you format dates! (read "format" as in library.getDate("YYYY-MM-DD.........");)

EDIT:

Moment.js seems the thing you're looking for: http://momentjs.com/

momentjs toISOString without the "z"

You can use momentjs timezone:
http://momentjs.com/timezone/

var newYork    = moment.tz("2014-06-01 12:00", "America/New_York");
var london = newYork.clone().tz("Europe/London");

newYork.format(); // 2014-06-01T12:00:00-04:00
london.format(); // 2014-06-01T17:00:00+01:00

The z inidcates an UTC timestamp, the API is expecting the difference to UTC and therefor the -4:00. If you do want conversions between timezones momentjs timezone is my suggested way to go.

But doesn't moment().format(); returns the time as 2014-09-08T08:02:17-05:00 ?



Related Topics



Leave a reply



Submit