js中获取时间

demo 测试:



    var date = new Date();

    function formatMonth(value) {
        return (value + 1) < 10 ? '0' + (value + 1) : (value + 1);
    }

    function format(value) {
        return (value) < 10 ? '0' + (value) : (value);
    }

    var localTime = date.getFullYear() + '-' +
        formatMonth(date.getMonth()) + '-' +
        (date.getDate()) + ' ' +
        format(date.getHours()) + ':' +
        format(date.getMinutes()) + ':' +
        format(date.getSeconds()) + ' ' +
        date.getMilliseconds();
    console.log(localTime);
    //2020-10-21 14:47:29 561

    var uTCTime = date.getUTCFullYear() + '-' +
        formatMonth(date.getUTCMonth()) + '-' +
        (date.getUTCDate()) + ' ' +
        format(date.getUTCHours()) + ':' +
        format(date.getUTCMinutes()) + ':' +
        format(date.getUTCSeconds()) + ' ' +
        date.getUTCMilliseconds();
    console.log(uTCTime);
    //2020-10-21 06:47:29 561

    console.log('==================================================');

    console.log(date.getDay());   //3 ,本地周几
    console.log(date.getUTCDay());//3 ,utc周几
    console.log('-------------------day');

    console.log('==================================================');

    //时间戳
    console.log(date.getTime());//1603252737054
    //获取当前时间(从1970.1.1开始的毫秒数)

    console.log(date.getTimezoneOffset());//-480 min
    console.log(date.getTimezoneOffset() / 60);//-8 h
    //格林威治时间和本地时间之间的时差,以分钟为单位。
    console.log('-------------------time');

    //获取时区
    console.log(Intl.DateTimeFormat().resolvedOptions().timeZone);//Asia/Shanghai

    console.log('UTC+' + (0 - new Date().getTimezoneOffset() / 60)); // 输出:UTC+8

    console.log('==================================================');
    //时间
    console.log(date.toString());
    console.log(date.toTimeString());
    console.log(date.toDateString());
    console.log('-------------------无Locale');
    // Wed Oct 21 2020 14:47:29 GMT+0800 (中国标准时间)
    // 14:47:29 GMT+0800 (中国标准时间)
    // Wed Oct 21 2020

    //时间
    console.log(date.toLocaleString());
    console.log(date.toLocaleTimeString());
    console.log(date.toLocaleDateString());
    console.log('-------------------有Locale');
    // 2020/10/21 下午2:47:29
    // 下午2:47:29
    // 2020/10/21
    console.log('==================================================');


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值