• 微信公众号:美女很有趣。 工作之余,放松一下,关注即送10G+美女照片!

JS获取当前时间并格式化"yyyy-MM-dd HH:mm:ss

互联网 diligentman 2周前 (04-09) 12次浏览

方法一:

Date.prototype.format = function (format) {
           var args = {
               "M+": this.getMonth() + 1,
               "d+": this.getDate(),
               "h+": this.getHours(),
               "m+": this.getMinutes(),
               "s+": this.getSeconds(),
               "q+": Math.floor((this.getMonth() + 3) / 3),  //quarter
               "S": this.getMilliseconds()
           };
           if (/(y+)/.test(format))
               format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
           for (var i in args) {
               var n = args[i];
               if (new RegExp("(" + i + ")").test(format))
                   format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? n : ("00" + n).substr(("" + n).length));
           }
           return format;
       };
 
//调用方法
alert(new Date().format("yyyy-MM-dd hh:mm:ss:S"));
alert(new Date().format("yyyy-MM-dd hh:mm:ss"));

方法二:

function getNowFormatDate() {
    var date = new Date();
    var seperator1 = "-";
    var seperator2 = ":";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = date.getYear() + seperator1 + month + seperator1 + strDate
            + " " + date.getHours() + seperator2 + date.getMinutes()
            + seperator2 + date.getSeconds();
    return currentdate;
}

方法三:

function curDateTime(){
var d = new Date(); 
var year = d.getYear(); 
var month = d.getMonth()+1; 
var date = d.getDate(); 
var day = d.getDay(); 
var hours = d.getHours(); 
var minutes = d.getMinutes(); 
var seconds = d.getSeconds(); 
var ms = d.getMilliseconds(); 
var curDateTime= year;
if(month>9)
curDateTime = curDateTime +"-"+month;
else
curDateTime = curDateTime +"-0"+month;
if(date>9)
curDateTime = curDateTime +"-"+date;
else
curDateTime = curDateTime +"-0"+date;
if(hours>9)
curDateTime = curDateTime +""+hours;
else
curDateTime = curDateTime +"0"+hours;
if(minutes>9)
curDateTime = curDateTime +":"+minutes;
else
curDateTime = curDateTime +":0"+minutes;
if(seconds>9)
curDateTime = curDateTime +":"+seconds;
else
curDateTime = curDateTime +":0"+seconds;
return curDateTime; 
}
 
alert(curDateTime());

 

展开阅读全文

举报

打赏

0


0 收藏

微信
QQ
微博

分享

作者的其它热门文章

网易云音乐mp3外链、真实地址下载方法
win10运行bat文件 一闪而过 解决
Android 读取Assets中图片
layui 获取富文本内容


程序员灯塔
转载请注明原文链接:JS获取当前时间并格式化"yyyy-MM-dd HH:mm:ss
喜欢 (0)