Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to output date in javascript in ISO 8601 without milliseconds and with Z

Here is a standard way to serialise date as ISO 8601 string in JavaScript:

var now = new Date(); console.log( now.toISOString() ); // outputs '2015-12-02T21:45:22.279Z' 

I need just the same output, but without milliseconds. How can I output 2015-12-02T21:45:22Z

like image 916
bessarabov Avatar asked Dec 02 '15 21:12

bessarabov


People also ask

How do I remove T and Z from timestamp?

To remove the T and Z characters from an ISO date in JavaScript, we first need to have the date in ISO format. If you don't have it already, you can convert it to ISO using the toISOString function on a Date object. This will get rid of both T and Z, resulting in "2022-06-22 07:54:52.657".

How do I format a date in ISO 8601?

ISO 8601 represents date and time by starting with the year, followed by the month, the day, the hour, the minutes, seconds and milliseconds. For example, 2020-07-10 15:00:00.000, represents the 10th of July 2020 at 3 p.m. (in local time as there is no time zone offset specified—more on that below).

What is Z in ISO 8601 date format?

Z is the zone designator for the zero UTC offset. "09:30 UTC" is therefore represented as "09:30Z" or "T0930Z". "14:45:15 UTC" would be "14:45:15Z" or "T144515Z". The Z suffix in the ISO 8601 time representation is sometimes referred to as "Zulu time" because the same letter is used to designate the Zulu time zone.

Does ISO 8601 have milliseconds?

Date formatting On the contrary to the time zone, by default the number of milliseconds is not displayed. However, when displayed, the format is: "yyyy-MM-dd'T'hh:mm:ss. sss'Z'" if in UTC or "yyyy-MM-dd'T'hh:mm:ss. sss[+|-]hh:mm" otherwise.


1 Answers

Simple way:

console.log( new Date().toISOString().split('.')[0]+"Z" ); 
like image 61
Blue Eyed Behemoth Avatar answered Oct 11 '22 07:10

Blue Eyed Behemoth