javascript 日期 加减
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 日期 加减相关的知识,希望对你有一定的参考价值。
javascript 给一个日期增加45天,,,我搜索了半天怎么全都是不正确的代码呢,有做dateadd的,可是不正确,,,不准确 还能出2月30日 谁有准确的代码啊,,求啊!!!
用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_AddDays =45;
var LSTR_DateType = "YYYYMMDD";
alert(AddDate(LSTR_Date,LSTR_AddDays,LSTR_DateType));
function AddDate(LISTR_Date,LISTR_AddDays,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_AddDays,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;
elseLINT_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;
default:
return;
break;
if (LSTR_MM<10)
LSTR_MM="0" +LSTR_MM;
if (LSTR_DD<10)
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> 参考技术A //******************************************************//
// //
// 日期对象类库 //
// //
//******************************************************//
/*
* 类名: DateTime
* 说明: 兼容ASP.NET日期类型
* 接口:
* addDays(n) - 增加n天
*/
// Example:
// var d = new DateTime();
// var d = new DateTime(dateObject);
// var d = new DateTime(dateString);
// var d = new DateTime(year, month, date, hours, minutes, seconds,ms)
var CST_DATE_SPLIT = '-';
var CST_DATE_TIME_SPLIT = ' ';
var CST_TIME_SPLIT = ':';
/*
* 对象: DateTime
* 说明: 构造
* 参数:
* year - 日期实例
* year - 日期字符串
* year,month,day,hour,minute,second,ms - 年,月,日,时,分,秒,毫秒
*/
function DateTime(year, month, day, hour, minute, second, ms)
var d = new Date();
// 属性定义
this.Year = d.getFullYear();
this.Month = d.getMonth();
this.Day = d.getDay();
this.Hour = this.Minute = this.Second = this.Millisecond = 0;
// 方法定义
this.toString = function()
return digi(this.Year, 4) + CST_DATE_SPLIT +
digi(this.Month, 2) + CST_DATE_SPLIT +
digi(this.Day, 2) + CST_DATE_TIME_SPLIT +
digi(this.Hour, 2) + CST_TIME_SPLIT +
digi(this.Minute,2) + CST_TIME_SPLIT +
digi(this.Second,2) + CST_DATE_TIME_SPLIT +
digi(this.Millisecond, 3);
this.toDateString = function(splitter)
var s = '-';
if (splitter != null) s = splitter;
return digi(this.Year, 4) + ((s=='cn') ? '\u5E74' : s) +
digi(this.Month, 2) + ((s=='cn') ? '\u6708' : s) +
digi(this.Day, 2) + ((s=='cn') ? '\u65E5' : '');
this.toTimeString = function(splitter)
var s = ':';
if (splitter != null) s = splitter;
return digi(this.Hour, 2) + ((s=='cn') ? '\u65F6' : s) +
digi(this.Minute,2) + ((s=='cn') ? '\u5206' : s) +
digi(this.Second,2) + ((s=='cn') ? '\u79D2' : '');
this.toDateTimeString = function(splitter)
var s = ':';
if (splitter != null) s = splitter;
return digi(this.Year, 4) + ((s=='cn') ? '\u5E74' : s) +
digi(this.Month, 2) + ((s=='cn') ? '\u6708' : s) +
digi(this.Day, 2) + ((s=='cn') ? '\u65E5' : '') +
' ' +
digi(this.Hour, 2) + ((s=='cn') ? '\u65F6' : ':') +
digi(this.Minute,2) + ((s=='cn') ? '\u5206' : ':') +
digi(this.Second,2) + ((s=='cn') ? '\u79D2' : '');
this.clone = function(dt)
this.Year = dt.Year;
this.Month = dt.Month;
this.Day = dt.Day;
this.Hour = dt.Hour;
this.Minute = dt.Minute;
this.Second = dt.Second;
this.Millisecond = dt.Millisecond;
this.parseDate = function(date)
this.Year = date.getFullYear();
this.Month = (date.getMonth() + 1);
this.Day = date.getDate();
this.Hour = date.getHours();
this.Minute = date.getMinutes();
this.Second = date.getSeconds();
this.Millisecond = date.getMilliseconds()
this.toDate = function()
return new Date(this.Year, this.Month, this.Day, this.Hour, this.Minute, this.Second, this.Millisecond);
this.addDays = function(value)
var CST_DAY_MILLISECONDS = 86400000;
var vd = CST_DAY_MILLISECONDS * value;
var d = this.toDate();
d.setMilliseconds(d.getMilliseconds() + vd);
this.parseDate(d);
this.addYears
this.addMonths = function(value)
// to be do...
this.addMonths = function(value)
// to be do...
this.addHours = function(value)
this.addDays(value/24);
this.addMinutes = function(value)
this.addHours(value/60);
this.addSeconds = function(value)
this.addMinutes(value/60);
this.parseString = function(src)
var o = Date.parseDate(src);
if (o == null || o.getType == null || o.getType() != "Date")
return null;
else
this.parseDate(o);
// 实例化
var args = arguments;
if (args.length == 1)
var o = args[0];
if(typeof(o) == "string")
o = Date.parseDate(o);
if (o == null || o.getType == null || o.getType() != "Date")
return null;
else
this.parseDate(o);
else if (args.length >= 3)
this.Year = parseInt(args[0]);
this.Month = parseInt(args[1]);
this.Day = parseInt(args[2]);
if (args.length >= 4)
this.Hour = parseInt(args[3]);
if (args.length >= 5)
this.Minute= parseInt(args[4]);
if (args.length >= 6)
this.Second= parseInt(args[5]);
if (args.length >= 7)
this.Millisecond= parseInt(args[6]);
function digi(v, c)
v = v + "";
var n = "0000";
if(v.length < c)
return n.substr(0, c-v.length) + v;
return v;
DateTime.prototype = new Object();
DateTime.getType = function()
return "DateTime";
/*
* 函数: (静态)parseXml
* 说明: 从XML字符串解释
* 参数:
* xml - XML 描述
* 示例:
* <Object type="DateTime">2005-01-01</Object>
*/
DateTime.parseXml = function(xml, binary)
var doc = XmlDocument();
doc.loadXML(xml);
return new DateTime(doc.text);
//var xml = "<Object type=\"DateTime\">" + s1 + "</Object>";
参考技术B Js代码
<span style="font-size: small;">/**
* js 对date加减
*/
Date.prototype.Format = function(fmt)
// author: 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.addDays = function(d)
this.setDate(this.getDate() + d);
;
Date.prototype.addWeeks = function(w)
this.addDays(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);
;
//js格式化时间
Date.prototype.toDateString = function(formatStr)
var date = this;
var timeValues = function()
;
timeValues.prototype =
year : function()
if (formatStr.indexOf("yyyy") >= 0)
return date.getYear();
else
return date.getYear().toString().substr(2);
,
elseTime : function(val, formatVal)
return formatVal >= 0 ? (val < 10 ? "0" + val : val) : (val);
,
month : function()
return this.elseTime(date.getMonth() + 1, formatStr.indexOf("MM"));
,
day : function()
return this.elseTime(date.getDate(), formatStr.indexOf("dd"));
,
hour : function()
return this.elseTime(date.getHours(), formatStr.indexOf("hh"));
,
minute : function()
return this.elseTime(date.getMinutes(), formatStr.indexOf("mm"));
,
second : function()
return this.elseTime(date.getSeconds(), formatStr.indexOf("ss"));
var tV = new timeValues();
var replaceStr =
year : ["yyyy", "yy"],
month : ["MM", "M"],
day : ["dd", "d"],
hour : ["hh", "h"],
minute : ["mm", "m"],
second : ["ss", "s"]
;
for (var key in replaceStr)
formatStr = formatStr.replace(replaceStr[key][0], eval("tV." + key
+ "()"));
formatStr = formatStr.replace(replaceStr[key][1], eval("tV." + key
+ "()"));
return formatStr;
function formatStrDate(date)
var str = date.toDateString("yyyy-MM-dd hh:mm:ss");
if (str.indexOf("00:00:00") != -1)
return str.replace("00:00:00", "10:00:00");
return str;
var date = new Date();
date.addDays(-3);
console.info(date.toDateString("yyyy-MM-dd hh:mm:ss"));
</span> 参考技术C import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
long time = Calendar.getInstance().getTimeInMillis();
Date newDate = new Date(time + 45l * 24l * 3600l * 1000l);
//当前毫秒数 + 45天*24小时*3600秒*1000毫秒,这个地方的数字注意结尾要加字母l。
System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(newDate));
这个newDate就是你要的日期。 参考技术D javascript 时间日期处理相加,减操作方法js
<script language="JavaScript">
<!--
var d = new Date("2008/04/15");
d.setMonth(d.getMonth() + 1 + 1);//加一个月,同理,可以加一天:getDate()+1,加一年:getYear()+1
alert(d+"月后是"+d.getFullYear()+"-"+d.getMonth()+"-"+d.getDate());
//-->
</script>
javascript 日期 加减 增加45天
javascript 给一个日期增加45天,,,我搜索了半天怎么全都是不正确的代码呢,有做dateadd的,可是不正确,,,不准确 还能出2月30日 谁有准确的代码啊,,求啊!!!
参考技术A 具体做法如下(经测试答案正确):<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_AddDays =45;
var LSTR_DateType = "YYYYMMDD";
alert(AddDate(LSTR_Date,LSTR_AddDays,LSTR_DateType));
function AddDate(LISTR_Date,LISTR_AddDays,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_AddDays,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;
elseLINT_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;
default:
return;
break;
if (LSTR_MM<10)
LSTR_MM="0" +LSTR_MM;
if (LSTR_DD<10)
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>本回答被提问者采纳
以上是关于javascript 日期 加减的主要内容,如果未能解决你的问题,请参考以下文章