How js/jquery parses the timestamp to the specified format

clipboard.png

,

clipboard.png

I want to convert to

in yyyy-MM-dd HH:mm:ss format.
Mar.21,2021

use the library, date-fns or moment

or there is an old piece of code that extends the Date object

directly.
Date.prototype.format = function(format = "yyyy-MM-dd") {
    var o = {
        //month 
        "M+": this.getMonth() + 1,
        // day  + 1
        "d+": this.getDate(),
        // hour 24
        "H+": this.getHours(),
        // 12
        "h+": this.getHours() % 12,
        // minute 
        "m+": this.getMinutes(),
        // second 
        "s+": this.getSeconds(),
        // quarter 
        "q+": Math.floor((this.getMonth() + 3) / 3),
        // millisecond 
        "S": this.getMilliseconds()
    };

    if (/(y+)/i.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }

    for (var k in o) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
        }
    }
    return format;
};
Menu