Data对象API

学习分享2年前 (2022)更新 bestcyt
130 0 0
<script>
    /*
        Date对象:获取当前日期时间
     */
    var myDate= new Date();
    console.log ( myDate );//日期对象在打印的时候会自动转成事件字符串
    //1.转换格式
    console.log ( myDate.toLocaleTimeString () );
    console.log ( myDate.toLocaleString () );//2018/9/1 下午4:25:29
    //2.常用:获取年月日时分秒
    console.log ( myDate.getFullYear () );//2018  年
    //月份范围: 0-11  对应  1 - 12
    console.log ( myDate.getMonth () );//8(下标为8,第九个元素)   9月
    console.log ( myDate.getDate () );//1  日期
    //    星期 0-6     周日-周六
    console.log ( myDate.getDay () );//6  星期
    console.log ( myDate.getHours () );//16  时
    console.log ( myDate.getMinutes () );//30  分
    console.log ( myDate.getSeconds () );//11  秒
    console.log ( myDate.getMilliseconds () );//477  毫秒
    //3.创建自定义时间
    //3.1  参数分别对应:年月日时分秒
    var date1 = new Date(2017,1,5,10,30,10);//Sun Feb 05 2017 10:30:10 GMT+0800 (中国标准时间)
    console.log ( date1 );
    //3.2  参数传一个时间格式字符串  yyyy-mm-dd HH:mm:ss
    var date2 = new Date('2018-10-15 20:30:00');
    console.log ( date2 );
</script>

 

© 版权声明

相关文章