javascript获取当前时间CurentTime

Posted 壁虎漫步.

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript获取当前时间CurentTime相关的知识,希望对你有一定的参考价值。

function CurentTime(){ 
    var now = new Date();
    
    var year = now.getFullYear();       //
    var month = now.getMonth() + 1;     //
    var day = now.getDate();            //
    
    var hh = now.getHours();            //
    var mm = now.getMinutes();          //
    var ss = now.getSeconds();          //
    
    var clock = year + "-";
    
    if(month < 10)
        clock += "0";
    
    clock += month + "-";
    
    if(day < 10)
        clock += "0";
        
    clock += day + " ";
    
    if(hh < 10)
        clock += "0";
        
    clock += hh + ":";
    if (mm < 10) 
        clock += ‘0‘; 
 
    clock += mm + ":"; 
 
    if(ss < 10)
        clock += "0";
 
    clock +=ss;
    return(clock); 
}

得到的结果就是:2017-04-03 10:33:11

以上是关于javascript获取当前时间CurentTime的主要内容,如果未能解决你的问题,请参考以下文章

javaScript获取当前时间日期月份

javaScript获取当前时间

javascript 常用内置对象

JavaScript获取当前时间戳,时间戳转日期 - js

JavaScript获取当前日期时间及其它扩展操作(代码实现)

如何在 JavaScript 中获取当前日期?