保存为 DateExtend.js
//default format
Date.prototype.format="yyyy-MM-dd hh:mm:ss";
//var aa = new Date();
//aa.setFormatDate("2005-12-30","yyyy-MM-dd");
//aa.format = "yyyy-MM-dd";
//aa.setFormatDate("2006-11-11")
//[extended method] Date.setDate by format string
//Warning : date string must perfect than format string
String.prototype.toDate = function(format){return Date.getFormatDate(this.toString(),format);}
Date.prototype.setFormatDate = function(s,f){var self=this;self=Date.getFormatDate(s,f);}
Date.regex = {
re1:/([\^\$\/(){}*+?\-\.\[\]\\\|])/g
,re2:/(y+|M+|d+|m+|h+|s+)/g
,y1:/^y{1,2}$/
,y2:/^y{4}$/
,M:/^M{1,2}$/
,d:/^d{1,2}$/
,h:/^h{1,2}$/i
,m:/^m{1,2}$/
,s:/^s{1,2}$/
}
Date.getFormatDate=function(_string,_format) //author : gzdiablo
{
var d = new Date();
var inputStr = typeof(_string)=="string"?_string:null;
var sformat = _format?_format:d.format;
var re = new RegExp(sformat.replace(Date.regex.re1,"\\$1").replace(Date.regex.re2,"(\\d+)"));
var arr = sformat.match(Date.regex.re2);
var arr2 = inputStr.match(re);
if(arr2 instanceof Array && arr instanceof Array)
{
arr2.shift();
for(var i=0,l=arr.length;i<l&&typeof(arr2[i])=="string";i++)
switch(true)
{
case Date.regex.y1.test(arr[i]):d.setFullYear(arr2[i]*1+(Math.ceil(this.getFullYear()/100)*100));break;
case Date.regex.y2.test(arr[i]):d.setFullYear(arr2[i]*1);break;
case Date.regex.M.test(arr[i]):d.setMonth(arr2[i]*1-1);break;
case Date.regex.d.test(arr[i]):d.setDate(arr2[i]*1);break;
case Date.regex.h.test(arr[i]):d.setHours(arr2[i]*1);break;
case Date.regex.m.test(arr[i]):d.setMinutes(arr2[i]*1);break;
case Date.regex.s.test(arr[i]):d.setSeconds(arr2[i]*1);break;
}
return d;
}
return null;
}
//[extended method] Date.toString by format string
Date.prototype.Format = function(informat) //author: meizz
{
var inputStr = "";
var format = typeof(informat)=="string"?informat:this.format;
var o = {
"M+" : this.getMonth()+1,
"d+" : this.getDate(),
"h+" : this.getHours(),
"H+" : this.getHours(),
"m+" : this.getMinutes(),
"s+" : this.getSeconds()
}
if(/(y+)/.test(format))
format=format.replace(RegExp.$1,(this.getFullYear()+"").substr(4 - RegExp.$1.length));
for(var k in o)
if(new RegExp("("+ k +")").test(format))
format = format.replace(RegExp.$1,RegExp.$1.length==1?o[k]:("00"+ o[k]).substr((""+o[k]).length));
return format;
}
Date.prototype.monthDays = function()
{
var days = new Array();
var tempdate = new Date(this.getFullYear(),this.getMonth(),1);
for(var i=0;i<42;i++)
{
var tempint = i + 1 - (tempdate.getDay()==0?7:tempdate.getDay());
days.push(new Date(tempdate.getFullYear(),tempdate.getMonth(),tempint));
}
return days;
}//[extended method] Date Count Method
Date.prototype.addYear = function(num){if(!isNaN(num))this.setFullYear(this.getFullYear() + parseInt(num));return this;}
Date.prototype.addMonth = function(num){if(!isNaN(num))this.setMonth(this.getMonth() + parseInt(num));return this;}
Date.prototype.addDay = function(num){if(!isNaN(num))this.setDate(this.getDate() + parseInt(num));return this;}
Date.prototype.addHour = function(num){if(!isNaN(num))this.setHours(this.getHours() + parseInt(num));return this;}
Date.prototype.addMinute = function(num){if(!isNaN(num))this.setMinutes(this.getMinutes() + parseInt(num));return this;}
Date.prototype.addSecond = function(num){if(!isNaN(num))this.setSeconds(this.getSeconds() + parseInt(num));return this;}
Date.prototype.addMS = function(num){if(!isNaN(num))this.setMilliseconds(this.getMilliseconds() + parseInt(num));return this;}
Date.prototype.addWeek = function(num){if(!isNaN(num))this.setDate(this.getDate() + parseInt(num) * 7);return this;}调用:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head><body>
</body>
</html>
<script language="javascript" src="js/DateExtend.js" charset="gb2312"></script>
<script language="javascript">
//_pDateStr:生产日期字符串 _oMonths:保质期 _dateFormat:日期格式(可选) _nowDateStr:当前日期字符串(可选)
var productDate = function(_pDateStr,_oMonths,_dateFormat,_nowDateStr)
{
_dateFormat = typeof(_dateFormat)=="string"?_dateFormat:"yyyy-MM-dd";
_oMonths = isNaN(_oMonths)?1:parseInt(_oMonths);
var pDate,now,oDate;
if(typeof(_pDateStr)=="string")pDate = _pDateStr.toDate(_dateFormat);
if(typeof(_nowDateStr)=="string")now = _nowDateStr.toDate(_dateFormat);
if(!pDate)pDate = new Date();
if(!now)now = new Date();
oDate = new Date(pDate).addMonth(_oMonths);
//保质期
this.oMonths = _oMonths;
//生产日期
this.productDate = pDate.Format(_dateFormat);
//过期日期
this.expiredDate = oDate.Format(_dateFormat);
//已过月
this.monthsPassed = (now.getMonth() - pDate.getMonth()) + ((now.getFullYear() - pDate.getFullYear()) * 12);
//剩余月
this.monthsLeft = this.oMonths - this.monthsPassed;
//比例
this.percent = [Math.round(this.monthsPassed / this.oMonths * 100),"%"].join('');
}window.onload = function()
{
var aa = new productDate("2007-9-12",5,null,"2007-10-1");
var arr = [];
for(var o in aa)
arr[arr.length] = [o,aa[o]].join(' = ');
alert(arr.join("\n"));
}
</script>
//default format
Date.prototype.format="yyyy-MM-dd hh:mm:ss";
//var aa = new Date();
//aa.setFormatDate("2005-12-30","yyyy-MM-dd");
//aa.format = "yyyy-MM-dd";
//aa.setFormatDate("2006-11-11")
//[extended method] Date.setDate by format string
//Warning : date string must perfect than format string
String.prototype.toDate = function(format){return Date.getFormatDate(this.toString(),format);}
Date.prototype.setFormatDate = function(s,f){var self=this;self=Date.getFormatDate(s,f);}
Date.regex = {
re1:/([\^\$\/(){}*+?\-\.\[\]\\\|])/g
,re2:/(y+|M+|d+|m+|h+|s+)/g
,y1:/^y{1,2}$/
,y2:/^y{4}$/
,M:/^M{1,2}$/
,d:/^d{1,2}$/
,h:/^h{1,2}$/i
,m:/^m{1,2}$/
,s:/^s{1,2}$/
}
Date.getFormatDate=function(_string,_format) //author : gzdiablo
{
var d = new Date();
var inputStr = typeof(_string)=="string"?_string:null;
var sformat = _format?_format:d.format;
var re = new RegExp(sformat.replace(Date.regex.re1,"\\$1").replace(Date.regex.re2,"(\\d+)"));
var arr = sformat.match(Date.regex.re2);
var arr2 = inputStr.match(re);
if(arr2 instanceof Array && arr instanceof Array)
{
arr2.shift();
for(var i=0,l=arr.length;i<l&&typeof(arr2[i])=="string";i++)
switch(true)
{
case Date.regex.y1.test(arr[i]):d.setFullYear(arr2[i]*1+(Math.ceil(this.getFullYear()/100)*100));break;
case Date.regex.y2.test(arr[i]):d.setFullYear(arr2[i]*1);break;
case Date.regex.M.test(arr[i]):d.setMonth(arr2[i]*1-1);break;
case Date.regex.d.test(arr[i]):d.setDate(arr2[i]*1);break;
case Date.regex.h.test(arr[i]):d.setHours(arr2[i]*1);break;
case Date.regex.m.test(arr[i]):d.setMinutes(arr2[i]*1);break;
case Date.regex.s.test(arr[i]):d.setSeconds(arr2[i]*1);break;
}
return d;
}
return null;
}
//[extended method] Date.toString by format string
Date.prototype.Format = function(informat) //author: meizz
{
var inputStr = "";
var format = typeof(informat)=="string"?informat:this.format;
var o = {
"M+" : this.getMonth()+1,
"d+" : this.getDate(),
"h+" : this.getHours(),
"H+" : this.getHours(),
"m+" : this.getMinutes(),
"s+" : this.getSeconds()
}
if(/(y+)/.test(format))
format=format.replace(RegExp.$1,(this.getFullYear()+"").substr(4 - RegExp.$1.length));
for(var k in o)
if(new RegExp("("+ k +")").test(format))
format = format.replace(RegExp.$1,RegExp.$1.length==1?o[k]:("00"+ o[k]).substr((""+o[k]).length));
return format;
}
Date.prototype.monthDays = function()
{
var days = new Array();
var tempdate = new Date(this.getFullYear(),this.getMonth(),1);
for(var i=0;i<42;i++)
{
var tempint = i + 1 - (tempdate.getDay()==0?7:tempdate.getDay());
days.push(new Date(tempdate.getFullYear(),tempdate.getMonth(),tempint));
}
return days;
}//[extended method] Date Count Method
Date.prototype.addYear = function(num){if(!isNaN(num))this.setFullYear(this.getFullYear() + parseInt(num));return this;}
Date.prototype.addMonth = function(num){if(!isNaN(num))this.setMonth(this.getMonth() + parseInt(num));return this;}
Date.prototype.addDay = function(num){if(!isNaN(num))this.setDate(this.getDate() + parseInt(num));return this;}
Date.prototype.addHour = function(num){if(!isNaN(num))this.setHours(this.getHours() + parseInt(num));return this;}
Date.prototype.addMinute = function(num){if(!isNaN(num))this.setMinutes(this.getMinutes() + parseInt(num));return this;}
Date.prototype.addSecond = function(num){if(!isNaN(num))this.setSeconds(this.getSeconds() + parseInt(num));return this;}
Date.prototype.addMS = function(num){if(!isNaN(num))this.setMilliseconds(this.getMilliseconds() + parseInt(num));return this;}
Date.prototype.addWeek = function(num){if(!isNaN(num))this.setDate(this.getDate() + parseInt(num) * 7);return this;}调用:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head><body>
</body>
</html>
<script language="javascript" src="js/DateExtend.js" charset="gb2312"></script>
<script language="javascript">
//_pDateStr:生产日期字符串 _oMonths:保质期 _dateFormat:日期格式(可选) _nowDateStr:当前日期字符串(可选)
var productDate = function(_pDateStr,_oMonths,_dateFormat,_nowDateStr)
{
_dateFormat = typeof(_dateFormat)=="string"?_dateFormat:"yyyy-MM-dd";
_oMonths = isNaN(_oMonths)?1:parseInt(_oMonths);
var pDate,now,oDate;
if(typeof(_pDateStr)=="string")pDate = _pDateStr.toDate(_dateFormat);
if(typeof(_nowDateStr)=="string")now = _nowDateStr.toDate(_dateFormat);
if(!pDate)pDate = new Date();
if(!now)now = new Date();
oDate = new Date(pDate).addMonth(_oMonths);
//保质期
this.oMonths = _oMonths;
//生产日期
this.productDate = pDate.Format(_dateFormat);
//过期日期
this.expiredDate = oDate.Format(_dateFormat);
//已过月
this.monthsPassed = (now.getMonth() - pDate.getMonth()) + ((now.getFullYear() - pDate.getFullYear()) * 12);
//剩余月
this.monthsLeft = this.oMonths - this.monthsPassed;
//比例
this.percent = [Math.round(this.monthsPassed / this.oMonths * 100),"%"].join('');
}window.onload = function()
{
var aa = new productDate("2007-9-12",5,null,"2007-10-1");
var arr = [];
for(var o in aa)
arr[arr.length] = [o,aa[o]].join(' = ');
alert(arr.join("\n"));
}
</script>
<head>
<script>
function math()
{
var dueDate=document.getElementById("DueDate");
var expiredDate=document.getElementById("ExpiredDate");
var remainDate=document.getElementById("RemainDate");
var rate=document.getElementById("Rate");var now=new Date();
var n_year=now.getFullYear();
var n_month=now.getMonth()+1;
var n_day=now.getDay();var proDate=new Date(2007,1,1);
var p_year=proDate.getFullYear();
var p_month=proDate.getMonth()+1;
var p_day=proDate.getDate();var d_Date=new Date(2007,proDate.getMonth()+15+1,1);
var d_year=d_Date.getFullYear();
var d_month=d_Date.getMonth()+1;
var d_day=d_Date.getDate();dueDate.value=d_year+"-"+d_month+"-"+d_day;
expiredDate.value=(n_year-p_year)*12+(n_month-p_month)+((n_day>p_day)?0:(-1));
remainDate.value=15-expiredDate.value;
if(remainDate.value<0)
remainDate.value=0
rate.value=expiredDate.value/15;
if(rate.value>1)
rate.value=1;
}
</script>
</head>
<body onload="math()">
生產日期 2007-1-1 保質日期 15(月)
<table><tr>
<td>
到期日期<input type="text" id="DueDate">
</td>
</tr><tr>
<td>
已过日期<input type="text" id="ExpiredDate">
</td>
</tr>
<tr>
<td>
剩余日期<input type="text" id="RemainDate">
</td>
</tr>
<tr>
<td>
过期比例<input type="text" id="Rate">
</td>
</tr>
</table>
</body>
</html>
//调用中 productDate function 是生产日期的类window.onload = function()
{
//初始化一个productDate类; 参数1:生产日期 参数2:保质期 参数3:(可选)日期格式 参数4:(可选)当前日期
var aa = new productDate("2007-9-12",5,"yyyy-MM-dd","2007-10-1");
var bb = new productDate("2007-9-12",5);
var cc = new productDate("2007-9-12",5,"yyyy-MM-dd");
var dd = new productDate("2007-9-12",5,null,"2007-10-1");//aa.oMonths 保质期;
//aa.productDate 生产日期
//aa.expiredDate 过期日期
//aa.monthsPassed 已过日期(月)
//aa.monthsLeft 剩余日期(月)
//aa.percent 过期比率
//bb cc dd 也有相同的属性
var arr = [];
for(var o in aa)
arr[arr.length] = [o,aa[o]].join(' = ');
alert(arr.join("\n"));
}
<br />
保持期<input name='yue' type='text' id='yue' value="3"/>
(月)
<input type='button' onclick='cal();' value='calnow'/>
<br />
<br />
<div id='out'></div>
<script >
function cal(){
var temp = riqi.value.split('-'); //文本框内容
var date1=new Date(); //现在日期
var d=new Date(temp[0],temp[1]-1,temp[2]); //输入的日期,月减1
var m=temp[1]-1+yue.value*1;
var d2 = new Date(temp[0],m,temp[2]); //加上保质期后的日期
//d.setMonth(d.getMonth()+yue.value);
var m=d2.getMonth()+1;
alert("到期日期:"+d2.getFullYear()+"-"+m+"-"+d2.getDate());
var outstr= "到期日期:"+d2.getFullYear()+"-"+m+"-"+d2.getDate()+"<br/>";
var d3=new Date();
//alert(d3.getFullYear()+'-'+d3.getMonth()+ "-"+d3.getDate());
var passMonth=(d3.getFullYear()-temp[0])*12+d3.getMonth()-d.getMonth();
var leavings = yue.value-passMonth;
var passpercent = passMonth*100/yue.value;
outstr=outstr+'已经生产:'+passMonth+'个月<br/>剩余:'+leavings+'月<br/>过期比率:'+passpercent+'%';
out.innerHTML=outstr
//alert(d3.getMonth()+1)+((d3.getFullYear()-d.getFullYear())*12)-(d.getMonth()+1);
}
</script >