首页 > 代码库 > 【转】Javascript dateDiff函数

【转】Javascript dateDiff函数

    function dateDiff(interval, date1, date2)
    {
        var objInterval = {‘D‘ : 1000 * 60 * 60 * 24, ‘H‘ : 1000 * 60 * 60, ‘M‘ : 1000 * 60, ‘S‘ : 1000, ‘T‘ : 1};
         interval = interval.toUpperCase();
        var dt1 = Date.parse(date1.replace(/-/g, ‘/‘));
        var dt2 = Date.parse(date2.replace(/-/g, ‘/‘));
        try
        {
            return Math.round((dt2 - dt1) / eval(‘(objInterval.‘ + interval + ‘)‘));
         }
        catch (e)
        {
            return e.message;
         }
     }

说明:interval 取值: d (day), m(minutes), s(second), t(毫秒),不分区大小写
日期格式: yyyy (/-) (m)m (/-) (d)d
使用方式:alert(dateDiff(‘D‘, ‘2007-4-1‘, ‘2007/04/19‘));

 

转自:http://hi.baidu.com/zlwang/item/0736ff38979a4e637d034b20