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

前端 未结 4 444
离开以前
离开以前 2020-12-12 15:03

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

 T00:00:00.000Z

And how to parse it?

相关标签:
4条回答
  • 2020-12-12 15:14

    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/

    0 讨论(0)
  • 2020-12-12 15:18

    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`
    
    0 讨论(0)
  • 2020-12-12 15:20

    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());
    
    0 讨论(0)
  • 2020-12-12 15:27

    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

    0 讨论(0)
提交回复
热议问题