zoukankan      html  css  js  c++  java
  • JAVASCRIPT中 日期相减

    //JAVASCRIPT中 日期相减很麻烦 ,现在有现成的实现方法,拷贝过去就可以用了,方便

    //调用该方法(主方法)
    function dateDiff(date1, date2){
        var type1 = typeof date1, type2 = typeof date2;
        if(type1 == 'string')
        date1 = stringToTime(date1);
        else if(date1.getTime)
        date1 = date1.getTime();
        if(type2 == 'string')
        date2 = stringToTime(date2);
        else if(date2.getTime)
        date2 = date2.getTime();
        return (date1 - date2) / 1000;//结果是秒
    }

    //字符串转成Time(dateDiff)所需方法
    function stringToTime(string){
        var f = string.split(' ', 2);
        var d = (f[0] ? f[0] : '').split('-', 3);
        var t = (f[1] ? f[1] : '').split(':', 3);
        return (new Date(
        parseInt(d[0], 10) || null,
        (parseInt(d[1], 10) || 1)-1,
        parseInt(d[2], 10) || null,
        parseInt(t[0], 10) || null,
        parseInt(t[1], 10) || null,
        parseInt(t[2], 10) || null
        )).getTime();

    }

    //调用 dateDiff("2009-10-10 19:00:00","2009-10-10 18:00:00")

    返回的是秒钟


    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ago52030/archive/2009/12/11/4987199.aspx

  • 相关阅读:
    hdu3709(数位dp)
    2012天津E题
    2012天津C题
    hdu1754(splay)
    赤裸裸的splay平衡树
    hdu(预处理+线段树)
    uva11922(强行用rope替代spaly)
    lightoj 1370 欧拉函数
    poj3294 出现次数大于n/2 的公共子串
    poj2774 后缀数组2个字符串的最长公共子串
  • 原文地址:https://www.cnblogs.com/kevinge/p/1722613.html
Copyright © 2011-2022 走看看