JS 构造函数 Date

浪子不回头ぞ 提交于 2019-11-28 22:15:52
<script src="https://cdn.bootcss.com/moment.js/2.24.0/moment.js"></script>
    <script type="text/javascript">
    
        var d1 = new Date();
        var result = d1.toString();
        console.log(result);    //Wed Aug 28 2019 14:39:05 GMT+0800 (中国标准时间)


        var d1 = new Date();
        var result = d1.toLocaleString();
        console.log(result);    //2019/8/28 下午2:39:05

        
        var d1 = new Date();
        var result = d1.getTime();
        console.log(result);    //时间戳        1566974345508


        var d1 = new Date(1568974098332);
        var result = d1.toLocaleString();
        console.log(result);    //2019/9/20 下午6:08:18


        //引用第三方JS资源库实现日期输出
        var d1 = new Date();
        
        //Format Dates
        var result1 =moment().format("YYYY-MM-DD,h:mm:ss a");
        var result2 = moment().format("YYYY/MM/DD");
        var result3 = moment().format("dddd");
        var result4 = moment().format("MM-Do-YY");
        var result5 = moment().format("YYYY [escaped] YYYY");

        console.log("一",result1);    //2019-08-29,9:26:32 am
        console.log("二",result2);    //2019/08/29
        console.log("三",result3);    //Thursday
        console.log("四",result4);    //2019-08-29,9:26:32 am
        console.log("五",result5);    //2019 escaped 2019

        //Relative Time
        var fhz1 = moment("2011 01 24","YYYY MM DD").fromNow();
        var fhz2 = moment("2031 11 04","YYYY MM DD").fromNow();
        var fhz3 = moment().startOf("day").fromNow();
        var fhz4 = moment().endOf("day").fromNow();
        var fhz5 = moment().startOf("hour").fromNow();
        var fhz6 = moment().endOf("hour").fromNow();

        console.log("一",fhz1);    //9 years ago
        console.log("二",fhz2);    //in 12 years
        console.log("三",fhz3);    //10 hours ago
        console.log("四",fhz4);    //in 14 hours
        console.log("五",fhz5);    //40 minutes ago
        console.log("六",fhz6);    //in 20 minutes

        //Calendar Time
        var jg1 = moment().subtract(2, 'days').calendar();
        var jg2 = moment().subtract(31, 'days').calendar();
        var jg3 = moment().subtract(18, 'days').calendar();
        var jg4 = moment().subtract(29, 'days').calendar();
        var jg5 = moment().calendar();
        var jg6 = moment().add(1,"days").calendar();
        var jg7 = moment().add(5,"days").calendar();
        var jg8 = moment().add(23,"days").calendar();
        
        console.log("一",jg1);        //Last Tuesday at 9:49 AM
        console.log("二",jg2);        //07/29/2019
        console.log("三",jg3);        //08/11/2019        
        console.log("四",jg4);        //07/31/2019        
        console.log("五",jg5);        //Today at 9:49 AM        
        console.log("六",jg6);        //Tomorrow at 9:49 AM
        console.log("七",jg7);        //Tuesday at 9:49 AM
        console.log("八",jg8);        //09/21/2019
        
        //Multiple Locale Support
        var fh1 = moment.locale();
        var fh2 = moment().format('LT');
        var fh3 = moment().format('LTS');
        var fh4 = moment().format('L');
        var fh5 = moment().format('l');
        var fh6 = moment().format('LL');
        var fh7 = moment().format('ll');
        var fh8 = moment().format('LLL');
        var fh9 = moment().format('lll');
        var fh10 = moment().format('LLLL');
        var fh11 = moment().format('llll');
        
        console.log("一",fh1);    //en
        console.log("二",fh2);    //2:32 PM
        console.log("三",fh3);    //2:32:28 PM
        console.log("四",fh4);    // 08/29/2019
        console.log("五",fh5);    //8/29/2019
        console.log("六",fh6);    // August 29, 2019
        console.log("七",fh7);    //Aug 29, 2019
        console.log("八",fh8);    //August 29, 2019 2:34 PM
        console.log("九",fh9);    // Aug 29, 2019 2:35 PM
        console.log("十",fh10);    //Thursday, August 29, 2019 2:32 PM
        console.log("十一",fh11);    //Thu, Aug 29, 2019 2:35 PM
        
        
        
    </script>

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!