time formatted output

Topic description

Output the specified time
format description according to the given time format
For 2014.09.05 13:14:20
yyyy: year, 2014
yy: year, 14
MM: month, fill up two digits, 09
M: month, 9
dd: date, 05
d: date, 5
HH: 24 hours, 13
H: 24 hours, 13
hh: 12 hours, 01
h: 12 hours, 1
mm : minutes, filled with two digits, 14
m: minutes, 14
ss: seconds, filled with two digits, 20
s: seconds, 20
w: week, as ['day', 'one', 'two', 'three' , 'four', 'five', 'six'], the demo result is five

function formatDate(oDate, sFormation) {
    var add0 = function(num){
        if(num<10)
            return 0+""+num;
        elsereturn num;
            
 
    };
    var o = {
        "yyyy":oDate.getFullYear(),
        "yy":oDate.getFullYear()%100,  //年份的后两位
        "MM":add0(oDate.getMonth()+1),
        "M":oDate.getMonth()+1,
        "dd":add0(oDate.getDate()),
        "d":oDate.getDate(),
        "HH":add0(oDate.getHours()),
        "H":oDate.getHours(),
        "hh":add0(oDate.getHours()%12),
        "h":oDate.getHours()%12,
        "mm":add0(oDate.getMinutes()),
        "m":oDate.getMinutes(),
        "ss":add0(oDate.getSeconds()),
        "s":oDate.getSeconds(),
        "w":function(){
            var day = ["日","一","二","三","四","五","六"];
            return day[oDate.getDay()];
        }(),
    };
    for ( var k in o){   // Does sFormation contain k strings 
        sFormation = sFormation.replace(k,o[k]);
    }
    return sFormation;
}
alert(formatDate(new Date(), 'yyyy-MM-dd HH:mm:ss 星期w'));

 

Source: Niuke.com

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325195518&siteId=291194637