Reputation: 5317
Can someone, please, explain this type of format in javascript
T00:00:00.000Z
And how to parse it?
Upvotes: 172
Views: 902368
Reputation: 557
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:
Upvotes: 1
Reputation: 395
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());
Upvotes: 0
Reputation: 339
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/
Upvotes: 16
Reputation: 71
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
Upvotes: 1
Reputation: 2692
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
Upvotes: 1
Reputation: 6770
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());
Upvotes: 179
Reputation: 407
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/
Upvotes: 19