155

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

 T00:00:00.000Z

And how to parse it?

MrCodingB
  • 2,284
  • 1
  • 9
  • 22
Ali Akram
  • 4,803
  • 3
  • 29
  • 38

7 Answers7

167

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());
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
nanndoj
  • 6,580
  • 7
  • 30
  • 42
  • 3
    Close - you should use `getUTCHours`, `getUTCMinutes`, and `getUTCSeconds`. Otherwise, you're imparting the behavior of the local time zone, which will yield different results depending on which time zone, and on which date you pick - due to DST. – Matt Johnson-Pint Mar 09 '15 at 19:43
18

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/

gone43v3r
  • 397
  • 1
  • 6
  • 2
    You should be able to parse this directly in the moment constructor with an appropriate format string. (moment will assume the current date if it's not part of the value.) Also, you might consider `moment.utc(...)` so the resulting value is in the same UTC zone as the original value. – Matt Johnson-Pint Mar 09 '15 at 19:47
  • can you tell me how can we convert back to the original date version T00:00:00.000Z from the formated date version? – Chronoviser Jun 10 '21 at 10:05
  • Are you use this result will give you T00:00:00.000Z format? – Ugur Sep 06 '21 at 15:11
15

As one person may have already suggested,

I passed the 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/

that_developer
  • 319
  • 2
  • 9
  • Would be good to also link and recommend the usage of [moment.js](https://momentjs.com/) javascript library clearly while explaining the code above. – VKen Nov 05 '19 at 14:16
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/low-quality-posts/24496195) – Phil Tune Nov 05 '19 at 14:30
  • @PhilTune you are right, i think my brain was moving so quick that I technically was replying to comment and not the question – that_developer Nov 05 '19 at 20:28
1

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

frianH
  • 7,295
  • 6
  • 20
  • 45
Shahid Hussain Abbasi
  • 2,508
  • 16
  • 10
1

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
Dharman
  • 30,962
  • 25
  • 85
  • 135
Sujay Kundu
  • 71
  • 1
  • 1
  • 10
1

The existing answers have already solved this. What I also found helpful is this timestamp converter, which gives a quick way to play around with the ISO 8601 and other timestamps. It'll help you decode and encode a particular time:

https://time.lol/

Dan Roberts
  • 517
  • 7
  • 13
0

I had similar problems, I solved it by converting the date/time, before sending to the server, using:

Example: (UTC Brazil) I typed "15/12/2020 22:30:00" and it sent: '2020-12-16T01:30:00.000Z'. You convert using:

var date = new Date('2020-11-06T01:30:00.000Z');
console.log(date.toLocaleDateString());
console.log(date.toLocaleString());
console.log(date.toLocaleTimeString());
FXLima
  • 337
  • 2
  • 5