js 日期加上天数(或者月份数)

js 日期加上天数
摘自:http://topic.csdn.net/t/20050908/15/4257662.html

自己的实际代码:
//--cyj 2011-3-24 取得下次保养日期的间隔
function SetNextMaintDate()
{
var sourceObj = document.getElementById("txtMaintDate"); //--开始日期
var destObj = document.getElementById("txtNextMaintDate"); //--下一个日期

if (sourceObj.value !=="")
{
var v3 = new Date(sourceObj.value.replace(/-/g,"/")); //--构造一个date对象 cyj

2011-3-24
var MonthInc = GetValue("select ParamValue from D_ParamSetting where

ParamID=20","ParamValue");
var v4 = new Date(v3.getYear(), v3.getMonth() + parseInt(MonthInc)+1,v3.getDate());

//--加1的目的是因为月份是0表示1月份,所以要加1;cyj 2011-3-24
destObj.value = v4.getYear() + "-" + v4.getMonth() + "-" + v4.getDate();
}
else
{
destObj.value = "";
}



}
阅:这当中,使用到了parseInt字符串转换成整型的函数;

----------------------------------------------------------
Date对象的构造方法就行....

new Date(year,month,day,hours,minutes,seconds,milseconds)

各个参数,你可以随便的怎么样传入数值型参数,Date对象会自己帮你计算好日期...

var dat = new Date();
new Date(dat.getFullYear()-1,dat.getMonth(),dat.getDate());
获得去年的今天这一天的日期...
---------------------------------------------------------
<script >
Date.prototype.addDay=function (num)
{
this.setDate(this.getDate()+num);
return this;
}

Date.prototype.addMonth=function (num)
{
var tempDate=this.getDate();
this.setMonth(this.getMonth()+num);
if(tempDate!=this.getDate()) this.setDate(0);
return this;
}

Date.prototype.addYear=function (num)
{
var tempDate=this.getDate();
this.setYear(this.getYear()+num);
if(tempDate!=this.getDate()) this.setDate(0);
return this;
}


var d1;
d1=new Date( '2004/02/29 ');
alert(d1.addYear(1));

d1=new Date( '2004/01/31 ');
alert(d1.addMonth(1));

d1=new Date( '2004/02/29 ');
alert(d1.addDay(1));


</script>
--------------------------------------------------------

倒,你使用addYear()之后,得到一个新的Date对象实例,你就不能自己去提取出来以组合成那样的吗??

Date.prototype.toString=function(){
var date = new Date();

var year = date.getFullYear();
var month = date.getMonth()+1;
var day = date.getDate();

var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();

function _f(n){
if(n <10)
n = "0 "+n;
return n;
}

return year+ "- "+_f(month)+ "- "+_f(day)+ " "+_f(hours)+ ": "+_f(minutes)+ ": "+_f

(seconds);
}

用这段

alert(new Date());
结果就是你那样的格式.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值