zoukankan      html  css  js  c++  java
  • javascript的日期加减

    javascript的日期加减

    function TimeCom( dateValue )
    {
    var newCom = new Date( dateValue );
    this.year = newCom.getYear();
    this.month = newCom.getMonth()+1;
    this.day = newCom.getDate();
    this.hour = newCom.getHours();
    this.minute = newCom.getMinutes();
    this.second = newCom.getSeconds();
    this.msecond = newCom.getMilliseconds();
    this.week = newCom.getDay();
    }

    function DateDiff(interval,date1,date2)
    {
    var TimeCom1 = new TimeCom(date1);
    var TimeCom2 = new TimeCom(date2);
    var result;
    switch(String(interval).toLowerCase())
    {
    case "y":
    case "year":
    result = TimeCom1.year-TimeCom2.year;
    break;
    case "n":
    case "month":
    result = (TimeCom1.year-TimeCom2.year)*12+(TimeCom1.month-TimeCom2.month);
    break;
    case "d":
    case "day":
    result = Math.round((Date.UTC(TimeCom1.year,TimeCom1.month-1,TimeCom1.day)-Date.UTC(TimeCom2.year,TimeCom2.month-1,TimeCom2.day))/(1000*60*60*24));
    break;
    case "h":
    case "hour":
    result = Math.round((Date.UTC(TimeCom1.year,TimeCom1.month-1,TimeCom1.day,TimeCom1.hour)-Date.UTC(TimeCom2.year,TimeCom2.month-1,TimeCom2.day,TimeCom2.hour))/(1000*60*60));
    break;
    case "m":
    case "minute":
    result = Math.round((Date.UTC(TimeCom1.year,TimeCom1.month-1,TimeCom1.day,TimeCom1.hour,TimeCom1.minute)-Date.UTC(TimeCom2.year,TimeCom2.month-1,TimeCom2.day,TimeCom2.hour,TimeCom2.minute))/(1000*60));
    break;
    case "s":
    case "second":
    result = Math.round((Date.UTC(TimeCom1.year,TimeCom1.month-1,TimeCom1.day,TimeCom1.hour,TimeCom1.minute,TimeCom1.second)-Date.UTC(TimeCom2.year,TimeCom2.month-1,TimeCom2.day,TimeCom2.hour,TimeCom2.minute,TimeCom2.second))/1000);
    break;
    case "ms":
    case "msecond":
    result = Date.UTC(TimeCom1.year,TimeCom1.month-1,TimeCom1.day,TimeCom1.hour,TimeCom1.minute,TimeCom1.second,TimeCom1.msecond)-Date.UTC(TimeCom2.year,TimeCom2.month-1,TimeCom2.day,TimeCom2.hour,TimeCom2.minute,TimeCom2.second,TimeCom1.msecond);
    break;
    case "w":
    case "week":
    result = Math.round((Date.UTC(TimeCom1.year,TimeCom1.month-1,TimeCom1.day)-Date.UTC(TimeCom2.year,TimeCom2.month-1,TimeCom2.day))/(1000*60*60*24)) % 7;
    break;
    default:
    result = "invalid";
    }
    return(result);
    }

    function DateAdd(interval, num, dateValue)
    {
    var newCom = new TimeCom(dateValue);
    switch(String(interval).toLowerCase())
    {
    case "y": case "year": newCom.year += num; break;
    case "n": case "month": newCom.month += num; break;
    case "d": case "day": newCom.day += num; break;
    case "h": case "hour": newCom.hour += num; break;
    case "m": case "minute": newCom.minute += num; break;
    case "s": case "second": newCom.second += num; break;
    case "ms": case "msecond": newCom.msecond += num; break;
    case "w": case "week": newCom.day += num*7; break;
    default: return("invalid");
    }
    var now = newCom.year+"/"+newCom.month+"/"+newCom.day+" "+newCom.hour+":"+newCom.minute+":"+newCom.second;
    return(new Date(now));
    }

    2.另一个简单例子
    日期减去天数等于第二个日期
    <script language=Javascript>
    function cc(dd,dadd)
    {
    //可以加上错误处理
    var a = new Date(dd)
    a = a.valueOf()
    a = a - dadd * 24 * 60 * 60 * 1000
    a = new Date(a)
    alert(a.getFullYear() + "年" + (a.getMonth() + 1) + "月" + a.getDate() + "日")
    }
    cc("12/23/2002",2)
    </script>
    这里不得不做补充,浪费好多时间得出教训:
    Javascript 对时间的代号
    0-11数字表示1-12月: var a= new Date(2006,5,6) 结果是2006-6-6
    0-6表示星期
    1-31表示日期
    0-23小时
    0-59分钟,秒

    原文地址:http://www.cnblogs.com/linbaba/archive/2006/12/05/583173.html

  • 相关阅读:
    unity xcode debug 学习
    Amplify Shader Editor 学习
    Unity Quality Setting 学习
    Unity3D Distortion 学习
    【原】博客园第三方客户端-i博客园App开源
    【AR实验室】mulberryAR:并行提取ORB特征
    【AR实验室】mulberryAR :添加连续图像作为输入
    【原】C++11并行计算 — 数组求和
    【AR实验室】mulberryAR : ORBSLAM2+VVSION
    【AR实验室】OpenGL ES绘制相机(OpenGL ES 1.0版本)
  • 原文地址:https://www.cnblogs.com/suizhikuo/p/2366407.html
Copyright © 2011-2022 走看看