Format Date as “yyyy-MM-dd'T'HH:mm:ss.SSS'Z'”

后端 未结 5 557
囚心锁ツ
囚心锁ツ 2021-02-02 05:49

I need to format a date as yyyy-MM-dd\'T\'HH:mm:ss.SSS\'Z\' as specified by Parse\'s REST API for Facebook. I was wondering what the most lightweight

相关标签:
5条回答
  • 2021-02-02 06:08

    Add another option, maybe not the most lightweight.

    dayjs.extend(dayjs_plugin_customParseFormat)
    console.log(dayjs('2018-09-06 17:00:00').format( 'YYYY-MM-DDTHH:mm:ss.000ZZ'))
    <script src="https://cdn.jsdelivr.net/npm/dayjs@1.9.7/dayjs.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/dayjs@1.9.7/plugin/customParseFormat.js"></script>

    0 讨论(0)
  • toISOString() will return current UTC time only not the current local time. If you want to get the current local time in yyyy-MM-ddTHH:mm:ss.SSSZ format then you should get the current time using following two methods

    Method 1:

    document.write(new Date(new Date().toString().split('GMT')[0]+' UTC').toISOString());

    Method 2:

    document.write(new Date(new Date().getTime() - new Date().getTimezoneOffset() * 60000).toISOString());

    0 讨论(0)
  • 2021-02-02 06:19

    You can use javax.xml.bind.DatatypeConverter class

    DatatypeConverter.printDateTime & DatatypeConverter.parseDateTime

    0 讨论(0)
  • 2021-02-02 06:20
    function converToLocalTime(serverDate) {
    
        var dt = new Date(Date.parse(serverDate));
        var localDate = dt;
        
        var gmt = localDate;
            var min = gmt.getTime() / 1000 / 60; // convert gmt date to minutes
            var localNow = new Date().getTimezoneOffset(); // get the timezone
            // offset in minutes
            var localTime = min - localNow; // get the local time
    
        var dateStr = new Date(localTime * 1000 * 60);
        // dateStr = dateStr.toISOString("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); // this will return as just the server date format i.e., yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
        dateStr = dateStr.toString("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        return dateStr;
    }
    
    0 讨论(0)
  • 2021-02-02 06:22

    Call the toISOString() method:

    var dt = new Date("30 July 2010 15:05 UTC");
    document.write(dt.toISOString());
    
    // Output:
    //  2010-07-30T15:05:00.000Z
    
    0 讨论(0)
提交回复
热议问题