how to get formatted date time like 2009-05-29 21:55:57 using javascript?

前端 未结 10 1248
轮回少年
轮回少年 2021-02-01 14:33

when using new Date,I get something like follows:

Fri May 29 2009 22:39:02 GMT+0800 (China Standard Time)

but what I want is xxxx-xx-xx xx:xx:xx formatted time s

10条回答
  •  情歌与酒
    2021-02-01 15:13

    What you are looking for is toISOString that will be a part of ECMAScript Fifth Edition. In the meantime you could simply use the toJSON method found in json2.js from json.org.

    The portion of interest to you would be:

    Date.prototype.toJSON = function (key) {
      function f(n) {
        // Format integers to have at least two digits.
        return n < 10 ? '0' + n : n;
      }
      return this.getUTCFullYear()   + '-' +
           f(this.getUTCMonth() + 1) + '-' +
           f(this.getUTCDate())      + 'T' +
           f(this.getUTCHours())     + ':' +
           f(this.getUTCMinutes())   + ':' +
           f(this.getUTCSeconds())   + 'Z';
    };
    

提交回复
热议问题