js获取当前时间(包含am pm)

var fn = function(){
    var now = new Date;
    var y = now.getFullYear();
    var m = now.getMonth();
    var d = now.getDate();
    var h = now.getHours();
    var mm = now.getMinutes();
    var s = now.getSeconds();
    var str;
    if(h>12) {
        h -= 12;
        str = " PM";
    }else{
        str = " AM";
    }
    h = h < 10 ? "0" + h : h;
    d = d < 10 ? "0" + d : d;
    m = m < 10 ? "0" + m : m;
    mm = mm < 10 ? "0" + mm : mm;
    s = s < 10 ? "0" + s : s;
    var xy = y + "/" + m + "/" + d + "," + h + ":" + mm + ":" + s;
    xy += str;
    return xy;
};
 
console.info(fn());

 

posted @ 2018-05-18 10:58  夏天的鱼呀  阅读(4419)  评论(0编辑  收藏  举报