How do I output an ISO 8601 formatted string in JavaScript?

后端 未结 14 1312
小鲜肉
小鲜肉 2020-11-22 08:41

I have a Date object. How do I render the title portion of the following snippet?



        
相关标签:
14条回答
  • 2020-11-22 09:24
    function timeStr(d) { 
      return ''+
        d.getFullYear()+
        ('0'+(d.getMonth()+1)).slice(-2)+
        ('0'+d.getDate()).slice(-2)+
        ('0'+d.getHours()).slice(-2)+
        ('0'+d.getMinutes()).slice(-2)+
        ('0'+d.getSeconds()).slice(-2);
    }
    
    0 讨论(0)
  • 2020-11-22 09:25

    The question asked was ISO format with reduced precision. Voila:

     new Date().toISOString().slice(0, 19) + 'Z'
     // '2014-10-23T13:18:06Z'
    

    Assuming the trailing Z is wanted, otherwise just omit.

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