zoukankan      html  css  js  c++  java
  • 3.javascript转换日期字符串为Date对象

    js中文网

    阮一峰

    1.求format“xxxx年xx月xx日 xx:xx”类型的两个日期天数差

    var start = "2017年09月17日 13:51";
    var end = "2017年09月20日 16:53";
    function getTimeDifference(start,end){
        var startDate = new Date(Date.parse(start.replace(/[年|月]/g, '/').replace(/日/g,'')));
        var endDate = new Date(Date.parse(end.replace(/[年|月]/g, '/').replace(/日/g,'')));
        var timeDif = endDate.getTime()-startDate.getTime();
        var resultDay = timeDif/1000/60/60/24;
        return resultDay;
    }

     2.Date的long类型获取方法

    Date.now();
    new Date().getTime();
    new Date().valueOf();

    返回自 1970-1-1 00:00:00  UTC (世界标准时间)至今所经过的毫秒数。

    3.Date中的parse方法

    Date.parse("xxxx/xx/xx");//获取该string类型日期的毫秒数(返回的是本地时间)
    Date.parse("xxxx-xx-xx");//获取该string类型日期的毫秒数(返回自 1970-1-1 00:00:00  UTC (世界标准时间)至今所经过的毫秒数=本地时间+8小时)
    new Date("xxxx/xx/xx");//获取该string类型日期的date值
    new Date("xxxx/xx/xx").getTime();//获取该string类型日期的毫秒数

     注:对于YYYY-MM-DD形式的字符串,JavaScript引擎可能会将其当作ISO格式来解析,采用格林尼治时区作为计时标准;而对于其他格式的日期字符串,一律视为非ISO格式,采用本地时区作为计时标准。两者之间相差8小时。

    new Date('2014-01-01')
    // Wed Jan 01 2014 08:00:00 GMT+0800 (CST)
    
    new Date('2014-1-1')
    // Wed Jan 01 2014 00:00:00 GMT+0800 (CST)
  • 相关阅读:
    【软件】Linux图形软件VNC&X11
    【C++语法】STL
    【C++语法】Type & Value Category
    【C++语法】关键字
    【C++语法】C++语法目录
    【算法·Algorithms】 Sort
    【代码·Patten】theme: Calculator
    centos MIT 6.828 qemu 安装问题
    【归纳】Layui table.render里的json后台传入
    【归纳】springboot中的IOC注解:注册bean和使用bean
  • 原文地址:https://www.cnblogs.com/zheaven/p/7566792.html
Copyright © 2011-2022 走看看