推广 热搜: ACF胶  回收ACF  收购ACF  200*16防溢裙板  济宁防溢裙板  求购ACF  挡尘帘  @2022已最新(今日/知乎)  AH0.6/12矿用按钮箱  GLD2200/7.5/S甲带给煤机 

js日期加减 、js日期相减

   日期:2023-04-22     浏览:51    评论:0    
核心提示:怎么让js中date进行加减运算js中没有如同C#中的AddD***s的方法,所以重写了Date对象的prototype,扩展了增加日期的方法,代码如下:Date.prototype.Format =

怎么让js中date进行加减运算

js中没有如同C#中的AddD***s的方法,

所以重写了Date对象的prototype,扩展了增加日期的方法,

代码如下:

Date.prototype.Format = function(fmt)

{

//***thor:

meizz

var o

=

{

"M+" : this.getMonth() + 1, //月份

"d+" : this.getDate(), //日

"h+" : this.getHours(), //小时

"m+" : this.getMinutes(), //分

"s+" : this.getSeconds(), //秒

"q+" : Math.floor((this.getMonth() + 3) / 3), //季度

"S" : this.getMilliseconds() //毫秒

};

if

(/(y+)/.test(fmt))

fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 -

RegExp.$1.length));

for (var k

in o)

if (new RegExp("(" + k + ")").test(fmt))

fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) :

(("00" + o[k]).substr(("" + o[k]).length)));

return

fmt;

}

Date.prototype.addD***s = function(d)

{

this.setDate(this.getDate() + d);

};

Date.prototype.addWeeks = function(w)

{

this.addD***s(w * 7);

};

Date.prototype.addMonths= function(m)

{

var d =

this.getDate();

this.setMonth(this.getMonth() + m);

if

(this.getDate() d)

this.setDate(0);

};

Date.prototype.addYears = function(y)

{

var m =

this.getMonth();

this.setFullYear(this.getFullYear() + y);

if (m

this.getMonth())

{

this.setDate(0);

}

};

var now = new Date();

now.addD***s(1);//加减日期操作

alert(now.Format("yyyy-MM-dd"));

js中对日期进行加减

var tod***=new Date(); // 获取今天时间

tod***.setDate(tod***.getDate() + 7); // 系统会自动转换

下面是date类提供的三个你可能生成字符串用到的函数:

getDate() 从 Date 对象返回一个月中的某一天 (1 ~ 31)。

getMonth() 从 Date 对象返回月份 (0 ~ 11)。

getFullYear() 从 Date 对象以四位数字返回年份。

如何用js进行日期的加减

如题,开始查了查js的使用文档,但没发现可以直接用的函数,于是就想自己写函数来着,这就要涉及到每个月天数的判断,如果是2月份的话,还要涉及到闰年的判断,虽然不复杂但我想js应该不会这么低级,于是查了下资料,终于有了如下重大发现,以在某个日期上加减天数来说,其实只要调用Date对象的setDate()函数就可以了,具体方法如下:

function addDate(date,d***s){

var d=new Date(date);

d.setDate(d.getDate()+d***s);

var m=d.getMonth()+1;

return d.getFullYear()+'-'+m+'-'+d.getDate();

}

其中,date参数是要进行加减的日期,d***s参数是要加减的天数,如果往前算就传入负数,往后算就传入正数,如果是要进行月份的加减,就调用setMonth()和getMonth()就可以了,需要注意的是返回的月份是从0开始计算的,也就是说返回的月份要比实际月份少一个月,因此要相应的加上1。

javascript 日期 加减

用Javascript实现(经测试答案正确):

html

head

meta http-equiv="Content-Type" content="text/html; charset=uft-8"

script language="javascript"

function window_onload()

{

var LSTR_Date = "20090115";

//(日期表示只有这一种格式,不支持民国年)

var LSTR_AddD***s =45;

var LSTR_DateType = "YYYYMMDD";

alert(AddDate(LSTR_Date,LSTR_AddD***s,LSTR_DateType));

}

function AddDate(LISTR_Date,LISTR_AddD***s,LISTR_DateType)

{

var LSTR_YY=0;

var LSTR_MM=0;

var LSTR_DD=0;

var LINT_FLAG=0;

//检查日期格式为 "YYYYMMDD" 或

//"MMDDYYYY" 且长度为8码

if((LISTR_DateType!="YYYYMMDD") (LISTR_DateType!="MMDDYYYY") (LISTR_Date.length!=8))

return false;

if(LISTR_DateType=="MMDDYYYY")

LISTR_Date=LISTR_Date.substr(4,4)+LISTR_Date.substr(0,4);

LSTR_YY=parseInt(LISTR_Date.substr(0,4),10);

LSTR_MM=parseInt(LISTR_Date.substr(4,2),10);

LSTR_DD=parseInt(LISTR_Date.substr(6,2),10)+parseInt(LISTR_AddD***s,10);

while(LINT_FLAG==0)

{

switch (LSTR_MM)

{

case 2:

if ((LSTR_YY % 4) != 0)

{

if (LSTR_DD 28)

{

LSTR_DD -=28;

LSTR_MM =3;

}

else

{

LINT_FLAG=1;

}

}

else

{

if (((LSTR_YY % 100) == 0) ((LSTR_YY % 400) != 0))

{

if (LSTR_DD 28)

{

LSTR_DD -=28;

LSTR_MM =3;

}

else

{

LINT_FLAG=1;

}

}

else

{

if (LSTR_DD 29)

{

LSTR_DD -=29;

LSTR_MM =3;

}

else

{

LINT_FLAG=1;

}

}

}

break;

case 4:

case 6:

case 9:

case 11:

if (LSTR_DD 30)

{

LSTR_DD -=30;

LSTR_MM +=1;

}

else{LINT_FLAG=1;}

break;

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

if (LSTR_DD 31)

{

LSTR_DD -=31;

LSTR_MM +=1;

}

else

{

LINT_FLAG=1;

}

break;

case 12:

if (LSTR_DD 31)

{

LSTR_DD -=31;

LSTR_MM=1;

LSTR_YY +=1;

}

else

{

LINT_FLAG=1;

}

break;

def***lt:

return;

break;

}

}

if (LSTR_MM10)

{

LSTR_MM="0" +LSTR_MM;

}

if (LSTR_DD10)

{

LSTR_DD="0" +LSTR_DD;

}

if(LISTR_DateType=="MMDDYYYY")

return LSTR_MM+""+LSTR_DD+""+LSTR_YY+"";

else

return LSTR_YY+""+LSTR_MM+""+LSTR_DD+"";

}

/script

/head

body onload="window_onload();"

/body

/html

js日期加减的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于js日期相减、js日期加减的信息别忘了在本站进行查找喔。

原文链接:http://www.hzciic.com/news/show-28749.html,转载和复制请保留此链接。
以上就是关于js日期加减 、js日期相减全部的内容,关注我们,带您了解更多相关内容。
 
标签: 加减 日期 对象
打赏
 
更多>同类资讯
0相关评论

推荐资讯
网站首页  |  VIP套餐介绍  |  关于我们  |  联系方式  |  使用协议  |  版权隐私  |  手机版  |  SITEMAPS  |  网站地图  |  排名推广  |  广告服务  |  积分换礼  |  网站留言  |  RSS订阅  |  违规举报