Core Technology (Volume 2) 02, Chapter 1-Regular expression

2023-01-23   ES  

// Date format conversion, the date convert to a stringfunction dateFormat(date,format){
    var o = {
        "M+" : date.getMonth()+1, //month
        "d+" : date.getDate(),    //day
        "h+" : date.getHours(),   //hour
        "m+" : date.getMinutes(), //minute
        "s+" : date.getSeconds(), //second
        "q+" : Math.floor((date.getMonth()+3)/3),  //quarter
        "S" : date.getMilliseconds() //millisecond
    };
    if(/(y+)/.test(format)) format=format.replace(RegExp.$1, (date.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;
}

var now = new Date();
var date = dateFormat(now,'yyyy-MM-dd hh:mm:ss');
console.log(date);

source

Related Posts

LOGSTASH nested json/multi -layer JSON parsing configuration configuration zhcha

Collections.sort sorting two methods for list

OpenGL — Glulookat function detailed explanation The following section of

40 website design commonly used techniques JavaScriptpeter

Core Technology (Volume 2) 02, Chapter 1-Regular expression

Random Posts

python reptile: crawling WeChat articles Qingmu

django settings allow cross -domain access zpeng

4.4-Delete container

Culture summarized afterwards

POI import Excel analysis of various format unit grid public methods for finishing