当前位置: 代码迷 >> ASP.NET >> js怎么把"2006-01-01"格式转为日期格式,并比较大小
  详细解决方案

js怎么把"2006-01-01"格式转为日期格式,并比较大小

热度:9818   发布时间:2013-02-25 00:00:00.0
js如何把"2006-01-01"格式转为日期格式,并比较大小
js如何把 "2006-01-01 "格式转为日期格式,并比较大小

------解决方案--------------------------------------------------------
s1=document.getElementById( "txtstart ").value.replace(/\-/g, "/ ") ;
var n1=new Date(s1);
s2=el( "txtend ").value.replace(/\-/g, "/ ") ;
var n2=new Date(s2);
if(n2 <n1)
{
alert( "结束日期不能小于开始日期 ");
return ;
}
------解决方案--------------------------------------------------------
/*用相对不规则的字符串来创建日期对象,不规则的含义为:顺序包含年月日三个数值串,有间隔*/
String.prototype.createDate = function(){
var reg = /^\D*(\d{2,4})\D+(\d{1,2})\D+(\d{1,2})\D*$/;
var str = this.replace(/\s/g, " ");
str = str.replace(reg, "$1/$2/$3 ");
var date = new Date(str);
if(isNaN(date)) return new Date();
else return date;
}

/*
* 功能:根据输入表达式返回日期字符串
* 参数:dateFmt:字符串,由以下结构组成
* yy:长写年,YY:短写年mm:数字月,MM:英文月,dd:日,hh:时,
* mi:分,ss秒,ms:毫秒,we:汉字星期,WE:英文星期.
* isFmtWithZero : 是否用0进行格式化,true or false
*/
Date.prototype.parseString = function(dateFmt,isFmtWithZero){
dateFmt = (dateFmt == null? "yy-mm-dd " : dateFmt);
isFmtWithZero = (isFmtWithZero == null?true : isFmtWithZero);
if(typeof(dateFmt) != "string " )
throw (new Error(-1, 'parseString()方法需要字符串类型参数! '));
var weekArr=[[ "星期日 ", "星期一 ", "星期二 ", "星期三 ", "星期四 ", "星期五 ", "星期六 "],
[ "SUN ", "MON ", "TUR ", "WED ", "THU ", "FRI ", "SAT "]];
var monthArr=[ "JAN ", "FEB ", "MAR ", "APR ", "MAY ", "JUN ", "JUL ", "AUG ", "SEP ", "OCT ", "NOV ", "DEC "];
var str=dateFmt;
str = str.replace(/yy/g,this.getFullYear());
str = str.replace(/YY/g,this.getYear());
str = str.replace(/mm/g,(this.getMonth()+1).toString().fmtWithZero(isFmtWithZero));
str = str.replace(/MM/g,monthArr[this.getMonth()]);
str = str.replace(/dd/g,this.getDate().toString().fmtWithZero(isFmtWithZero));
str = str.replace(/hh/g,this.getHours().toString().fmtWithZero(isFmtWithZero));
str = str.replace(/mi/g,this.getMinutes().toString().fmtWithZero(isFmtWithZero));
str = str.replace(/ss/g,this.getSeconds().toString().fmtWithZero(isFmtWithZero));
str = str.replace(/ms/g,this.getMilliseconds().toString().fmtWithZeroD(isFmtWithZero));
str = str.replace(/we/g,weekArr[0][this.getDay()]);
str = str.replace(/WE/g,weekArr[1][this.getDay()]);
return str;
}
/*将一位数字格式化成两位,如: 9 to 09*/
String.prototype.fmtWithZero = function(isFmtWithZero){
if(isFmtWithZero)
return (this <10? "0 "+this:this);
else return this;
}
String.prototype.fmtWithZeroD = function(isFmtWithZero){
if(isFmtWithZero)
return (this <10? "00 "+this:(this <100? "0 "+this:this));
else return this;
}

/* 功能 : 返回与某日期相距N天(N个24小时)的日期
* 参数 : num number类型 可以为正负整数或者浮点数,默认为1;
* type 0(秒) or 1(天),默认为秒
* 返回 : 新的PowerDate类型
*/
Date.prototype.dateAfter=function(num,type){
num = (num == null?1:num);
  相关解决方案