What does this format means T00:00:00.000Z?

JavascriptTimeTimezoneTimestampFormat

Javascript Problem Overview


Can someone, please, explain this type of format in javascript

 T00:00:00.000Z

And how to parse it?

Javascript Solutions


Solution 1 - Javascript

It's a part of ISO-8601 date representation. It's incomplete because a complete date representation in this pattern should also contains the date:

2015-03-04T00:00:00.000Z //Complete ISO-8601 date

If you try to parse this date as it is you will receive an Invalid Date error:

new Date('T00:00:00.000Z'); // Invalid Date

So, I guess the way to parse a timestamp in this format is to concat with any date

new Date('2015-03-04T00:00:00.000Z'); // Valid Date

Then you can extract only the part you want (timestamp part)

var d = new Date('2015-03-04T00:00:00.000Z');
console.log(d.getUTCHours()); // Hours
console.log(d.getUTCMinutes());
console.log(d.getUTCSeconds());

Solution 2 - Javascript

i suggest you use moment.js for this. In moment.js you can:

var localTime = moment().format('YYYY-MM-DD'); // store localTime
var proposedDate = localTime + "T00:00:00.000Z";

now that you have the right format for a time, parse it if it's valid:

var isValidDate = moment(proposedDate).isValid();
// returns true if valid and false if it is not.

and to get time parts you can do something like:

var momentDate = moment(proposedDate)
var hour = momentDate.hours();
var minutes = momentDate.minutes();
var seconds = momentDate.seconds();

// or you can use `.format`:
console.log(momentDate.format("YYYY-MM-DD hh:mm:ss A Z"));

More info about momentjs http://momentjs.com/

Solution 3 - Javascript

As one person may have already suggested,

I passed the https://en.wikipedia.org/wiki/ISO_8601">ISO 8601 date string directly to moment like so...

moment.utc('2019-11-03T05:00:00.000Z').format('MM/DD/YYYY')

or

moment('2019-11-03T05:00:00.000Z').utc().format('MM/DD/YYYY')

either of these solutions will give you the same result.

console.log(moment('2019-11-03T05:00:00.000Z').utc().format('MM/DD/YYYY')) // 11/3/2019

More info about momentjs http://momentjs.com/

Solution 4 - Javascript

Please use DateTimeFormatter ISO_DATE_TIME = DateTimeFormatter.ISO_DATE_TIME; instead of DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss") or any pattern

This fixed my problem Below

>java.time.format.DateTimeParseException: Text '2019-12-18T19:00:00.000Z' could not be parsed at index 10

Solution 5 - Javascript

Since someone asked how to implement it :

Its easy using momentjs :

// install using yarn
yarn add moment
// or install using npm 
npm i moment 

then you can do like this to extract the date according to the format you want :

import 'moment' from moment;

let isoDate = "2021-09-19T05:30:00.000Z";

let newDate =  moment.utc(isoDate).format('MM/DD/YY');
console.log('converted date', newDate); // 09/23/21 
 
let newDate2 = moment.utc(isoDate).format("MMM Do, YYYY");
console.log('converted date', newDate2); // Sept 24, 2021

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAli AkramView Question on Stackoverflow
Solution 1 - JavascriptnanndojView Answer on Stackoverflow
Solution 2 - Javascriptgone43v3rView Answer on Stackoverflow
Solution 3 - Javascriptthat_developerView Answer on Stackoverflow
Solution 4 - JavascriptShahid Hussain AbbasiView Answer on Stackoverflow
Solution 5 - JavascriptSujay KunduView Answer on Stackoverflow