What does this format means T00:00:00.000Z?
Solution 1
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
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
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/
Solution 4
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
Solution 5
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
Related videos on Youtube
Ali Akram
As an android developer I have responsibilities of working on medium to large scale applications and providing services for that working with experienced team members and leads.Along with development I have also responsibility in research field for providing the best possible solutions for setting up application architecture and developing application features to the development team and estimating the complexities of features.
Updated on July 08, 2022Comments
-
Ali Akram almost 2 years
Can someone, please, explain this type of format in javascript
T00:00:00.000Z
And how to parse it?
-
Drew about 9 years
-
Matt Johnson-Pint about 9 years
T
means "time", and usually separates the date from the time component.Z
means the value is in terms of UTC -
strattonn almost 3 yearsA date time that ends in Z is also called Zulu Time.
-
-
Matt Johnson-Pint about 9 yearsClose - you should use
getUTCHours
,getUTCMinutes
, andgetUTCSeconds
. 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 about 9 yearsYou 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. -
VKen over 4 yearsWould be good to also link and recommend the usage of moment.js javascript library clearly while explaining the code above.
-
Phil Tune over 4 yearsThis does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
-
that_developer over 4 years@PhilTune you are right, i think my brain was moving so quick that I technically was replying to comment and not the question
-
TarasH about 4 yearsIt's solved my problem with time convert from string type to OffsetDateTime
-
Chronoviser almost 3 yearscan you tell me how can we convert back to the original date version T00:00:00.000Z from the formated date version?
-
Ugur over 2 yearsAre you use this result will give you T00:00:00.000Z format?