• 【js】项目中有关时间的问题


    一、时间戳

    简单讲,unix时间戳就是从1970-01-01开始所经过的秒数,什么时候获取时间戳,就是到那个时间点所经历的秒数。

    二、JavaScript获取时间戳

    根据时间戳的定义,可以使用javascript中的几个方法来获取系统当前的时间戳:

    1、getTime()

    w3c school的解释如下:

    根据上面的定义,我们可以获取系统当前的时间戳:

    var timeStamp1 = new Date().getTime();
    console.info(timeStamp1);
    //输出:1556526945555
    
    

    2、parse()

    根据上面的解释,parse方法返回的是指定日期和时间到1970年1月1日的毫秒数,所以只要将日期指定为系统当前时间,就能获取系统当前的时间戳。

    //例子1:
    var timeStamp2 = Date.parse(new Date());
    console.log(timeStamp2);
    //输出:1556526945000

    3、valueOf()

      

    该方法返回的是Date对象的原始值,且和getTime返回相同的值,所以只要Date对象为系统当前时间,就能获取系统当前的时间戳

    var timeStamp5 = (new Date()).valueOf();
    console.info(timeStamp5); 
    //输出:1556527752836

     以上是获取系统当前时间的时间戳,如果要获取指定时间的时间戳,只需要设置指定日期,然后用对应的日期对象进行操作即可,不再赘述。

    从上面的结论可以看出,通过parse方法获取的时间戳后三位都是0,如果用更多的数据测试,都会发现该方法返回的时间戳没有其他两种方法精确,

    因为其后三位始终是0.

    <!doctype html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title>时间戳问题</title>
            <script>
                /***********************************************获取当前时间***********************************************/
                // 例子1:
                var timeStamp1 = (new Date()).getTime();
                console.log(timeStamp1);
                // 输出1556529963911
    
                // 例子8
                var timeStamp8 = (new Date()).valueOf();
                console.log(timeStamp8);
                //输出:1556529963914
    
                //例子9
                var timeStamp9 = +new Date();
                console.log(timeStamp9);
                //输出:1556529963914
    
                //例子10
                var timeStamp10 = Date.now();
                console.log(timeStamp10); 
                //输出:1556529963914
    
                // 
                console.log((new Date()).getTime() == +new Date());// 输出:true
                
                console.log((new Date()).getTime() == Date.now());// 输出:true
                
                console.log((new Date()).getTime() == (new Date()).valueOf());// 输出:true
                
                console.log((new Date()).getTime() == Date.parse(new Date()));// 输出:false
                
    
                /***********************************************Date.parse 获取自定义时间************************************************/ 
                // 例子2:
                var timeStamp2 = Date.parse(new Date());
                console.log(timeStamp2);
                // 输出:1556528914000
                
                // 例子3:
                var timeStamp3 = Date.parse('2019-4-29 00:00:00');
                console.log(timeStamp3);
                // 输出:1556467200000(chrome)  1556467200000(firefox)
    
                // 例子4
                var timeStamp4 = Date.parse('2019-4-29');
                console.log(timeStamp4);
                // 输出:1556467200000(chrome)  1556496000000(firefox)
    
                // 例子5
                var timeStamp5 = Date.parse('2019.4.29');
                console.log(timeStamp5);
                // 输出:1556467200000(chrome)  NaN(firefox)
    
                // 例子6
                var timeStamp6 = Date.parse('2019/4/29');
                console.log(timeStamp6);
                // 输出:1556467200000
    
    
                // 例子7
                var timeStamp7 = (new Date('2019.4.29')).valueOf();
                console.log(timeStamp7);
                //输出:1556467200000
    
            </script> 
        </head>
        <body>
        
        </body>
    </html>

    三、将时间戳转换为其他

    1.根据已知时间戳计算是周几

    function getWeekFn(value) {
        if (!value) return '';
        var newDate = new Date();
        newDate.setTime(parseInt(value));
        var temp = new Array("日", "一", "二", "三", "四", "五", "六");
        var week = new Date(newDate.toISOString()).getDay();
        return temp[week];
    };
    console.log(getWeekFn(Date.parse('2019.4.8')));
    console.log(getWeekFn(‘1554652800000’);
    执行结果:“一”
     

    2.根据给定日期计算周几

    function getWeekFn(value) {
        if (!value) return '';
        var newDate = new Date(value);
        var temp = new Array("日", "一", "二", "三", "四", "五", "六");
        var week = newDate.getDay();
        return temp[week];
    };

    例如:

           console.log(getWeekFn2('2019/4/8'))
      console.log(getWeekFn2('2019-4-8'))
      console.log(getWeekFn2('2019.4.8'))
    执行结果: “一”

    注意:该字符串应该能被 Date.parse() 正确方法识别

     相关资料:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Date

    作者:smile.轉角

    QQ:493177502

  • 相关阅读:
    并发编程 ~~~ 多进程~~~进程创建的两种方式, 进程pid, 验证进程之间的空间隔离, 进程对象join方法, 进程对象其他属性
    并发编程 ~~~ 多进程
    网络编程~~~~socketserver服务端
    网络编程~~~~粘包
    聊聊主流加密算法及该如何设计我们的用户密码
    Spring Boot Security 保护你的程序
    Spring Boot接口如何设计防篡改、防重放攻击
    Spring Boot 整合 Shiro实现认证及授权管理
    Spring Boot Quartz 分布式集群任务调度实现
    Spring Boot Redis 解析
  • 原文地址:https://www.cnblogs.com/websmile/p/10791752.html
Copyright © 2020-2023  润新知