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());
结果就是你那样的格式.
摘自: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());
结果就是你那样的格式.