javascript的日期加减

javascript的日期加减

function TimeCom( dateValue )
ExpandedBlockStart.gifContractedBlock.gifdot.gif{
InBlock.gif var newCom = new Date( dateValue );
InBlock.gif this.year = newCom.getYear();
InBlock.gif this.month = newCom.getMonth()+1;
InBlock.gif this.day = newCom.getDate();
InBlock.gif this.hour = newCom.getHours();
InBlock.gif this.minute = newCom.getMinutes();
InBlock.gif this.second = newCom.getSeconds();
InBlock.gif this.msecond = newCom.getMilliseconds();
InBlock.gif this.week = newCom.getDay();
ExpandedBlockEnd.gif }
None.gif
None.gif function DateDiff(interval,date1,date2)
ExpandedBlockStart.gifContractedBlock.gifdot.gif{
InBlock.gif var TimeCom1 = new TimeCom(date1);
InBlock.gif var TimeCom2 = new TimeCom(date2);
InBlock.gif var result;
InBlock.gif switch(String(interval).toLowerCase())
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif case "y":
InBlock.gif case "year":
InBlock.gif result = TimeCom1.year-TimeCom2.year;
InBlock.gif break;
InBlock.gif case "n":
InBlock.gif case "month":
InBlock.gif result = (TimeCom1.year-TimeCom2.year)*12+(TimeCom1.month-TimeCom2.month);
InBlock.gif break;
InBlock.gif case "d":
InBlock.gif case "day":
InBlock.gif 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));
InBlock.gif break;
InBlock.gif case "h":
InBlock.gif case "hour":
InBlock.gif 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));
InBlock.gif break;
InBlock.gif case "m":
InBlock.gif case "minute":
InBlock.gif 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));
InBlock.gif break;
InBlock.gif case "s":
InBlock.gif case "second":
InBlock.gif 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);
InBlock.gif break;
InBlock.gif case "ms":
InBlock.gif case "msecond":
InBlock.gif 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);
InBlock.gif break;
InBlock.gif case "w":
InBlock.gif case "week":
InBlock.gif 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;
InBlock.gif break;
InBlock.gif default:
InBlock.gif result = "invalid";
ExpandedSubBlockEnd.gif }
InBlock.gif return(result);
ExpandedBlockEnd.gif }
None.gif
None.gif function DateAdd(interval, num, dateValue)
ExpandedBlockStart.gifContractedBlock.gifdot.gif{
InBlock.gif var newCom = new TimeCom(dateValue);
InBlock.gif switch(String(interval).toLowerCase())
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif case "y": case "year": newCom.year += num; break;
InBlock.gif case "n": case "month": newCom.month += num; break;
InBlock.gif case "d": case "day": newCom.day += num; break;
InBlock.gif case "h": case "hour": newCom.hour += num; break;
InBlock.gif case "m": case "minute": newCom.minute += num; break;
InBlock.gif case "s": case "second": newCom.second += num; break;
InBlock.gif case "ms": case "msecond": newCom.msecond += num; break;
InBlock.gif case "w": case "week": newCom.day += num*7; break;
InBlock.gif default: return("invalid");
ExpandedSubBlockEnd.gif }
InBlock.gif var now = newCom.year+"/"+newCom.month+"/"+newCom.day+" "+newCom.hour+":"+newCom.minute+":"+newCom.second;
InBlock.gif return(new Date(now));
ExpandedBlockEnd.gif }

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值