JS获取打印当前时间

JS获取当前时间

Date 对象用于处理日期和时间。
创建 Date 对象的语法:var myDate=new Date()

年份:getFullYear()  从Date对象以四位数字返回年份

月份:getMonth()  从Date对象返回月份 (0 ~ 11)                                                                                   注意:【取月的时候取的是当前月-1,如果想取当前月+1就可以了】

日:getDate()   从Date对象返回一个月中的某一天 (1 ~ 31)

时分秒:hh:小时数,从0(午夜)到23(晚11点);mm:分钟数,从0到59的整数;ss:秒数,从0到59的整数
时分秒:【getHours() 返回 Date 对象的小时 (0 ~ 23);getMinutes() 返回 Date 对象的分钟 (0 ~ 59);getSeconds() 返回 Date 对象的秒数 (0 ~ 59)】

毫秒:getMilliseconds() 返回 Date 对象的毫秒(0 ~ 999)

时间戳/1000:getMilliseconds() getTime() 返回 1970 年 1 月 1 日至今的毫秒数

格式代码:月:MM 数字表示的月份,有前导零 01到12
                           M 数字表示的月份,没有前导零 1到12
                    日:D 月份中的第几天,没有前导零 1到31
                            DD 月份中的第几天,有前导零 01到31
                    年:YYYY 四位数字完整表示的年份 如:2014 或 2000
                             YY    两位数字表示的年份 如:14 或 98
                   时分秒:HH 小时,24小时制,有前导零 00到23
                                H 小时,24小时制,无前导零 0到23
                               hh 小时,12小时制,有前导零 00到12
                                 h 小时,12小时制,无前导零 0到12
                                m 没有前导零的分钟数 0到59
                               mm 有前导零的分钟数 00到59
                                  s 没有前导零的秒数 1到59
                                 ss 有前导零的描述 01到59

实例:function Time()
    {
        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 + "年";
       
     
       
        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);
}
var now_time =Time();
console.log(now_time)----->输出当前时间例(2014年12月3日 20:12:52)

猜你喜欢

转载自570109268.iteye.com/blog/2343562