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

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

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



        
14条回答
  •  旧时难觅i
    2020-11-22 09:01

    function getdatetime() {
        d = new Date();
        return (1e3-~d.getUTCMonth()*10+d.toUTCString()+1e3+d/1)
            .replace(/1(..)..*?(\d+)\D+(\d+).(\S+).*(...)/,'$3-$1-$2T$4.$5Z')
            .replace(/-(\d)T/,'-0$1T');
    }
    

    I found the basics on Stack Overflow somewhere (I believe it was part of some other Stack Exchange code golfing), and I improved it so it works on Internet Explorer 10 or earlier as well. It's ugly, but it gets the job done.

提交回复
热议问题