当前位置: 代码迷 >> JavaScript >> 注意了!日期急难有关问题!
  详细解决方案

注意了!日期急难有关问题!

热度:80   发布时间:2012-02-16 21:30:36.0
各位高手注意了!!!!日期急难问题!!!!
我现在   做了一个输入框,日期是自己手写进去的,格式为20070801,然后我点下一个输入框,可以让刚才那个输入框的日期自动+1,例如20070802,当然要有对月对日和对年的判断,不能有13月,32日等,还要考虑闰年的问题

------解决方案--------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN ">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME= "Generator " CONTENT= "EditPlus ">
<META NAME= "Author " CONTENT= " ">
<META NAME= "Keywords " CONTENT= " ">
<META NAME= "Description " CONTENT= " ">
</HEAD>

<BODY>
<script>

//日期+n天
function addDays(date1,days){

var monthDays=[0,3,0,1,0,1,0,0,1,0,0,1];
var regexp=/^(\d{1,4})[-|\.]{1}(\d{1,2})[-|\.]{1}(\d{1,2})$/;
regexp.test(date1);
var date1Year=RegExp.$1;
var date1Month=RegExp.$2;
var date1Day=RegExp.$3;
firstDate=new Date(date1Year,date1Month-1,date1Day);
alert(firstDate.toLocaleDateString())
var ccc=new Date(firstDate.getTime()+days*1000*60*60*24)
var result=ccc.getYear()+ "- "+(ccc.getMonth()+1)+ "- "+ccc.getDate();
alert(result)

}
addDays( '2007-08-09 ', '2 ')
addDays( '2007-01-31 ', '1 ')
</script>

<input type= "text " id= "aaa ">
<select id= "sel " style= "display:none ">
</select>
</BODY>
</HTML>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN ">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME= "Generator " CONTENT= "EditPlus ">
<META NAME= "Author " CONTENT= " ">
<META NAME= "Keywords " CONTENT= " ">
<META NAME= "Description " CONTENT= " ">
<script language=javascript>
String.prototype.isTime = function()
{
var r = this.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2})$/);
if(r==null)return false; var d = new Date(r[1], r[3]-1,r[4],r[5],r[6],0);
return (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]&&d.getHours()==r[5]&&d.getMinutes()==r[6]);
}
alert( "2002-1-31 12:34 ".isTime());
alert( "2001-2-29 12:54 ".isTime());
alert( "2002-1-41 12:00 ".isTime());

String.prototype.isDate = function()
{
var r = this.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
if(r==null)return false; var d = new Date(r[1], r[3]-1,r[4]);
return (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]);
}
alert( "2002-1-31 ".isDate());
alert( "2001-2-29 ".isDate());
alert( "2002-1-41 ".isDate());
</script>
</HEAD>

<BODY>

</BODY>
</HTML>

------解决方案--------------------
<html>
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
<title> new page </title>
<script>
function a(){
var str=document.all.txt1.value;
if(str.length==8){
var y=str.substring(0,4);
var m=str.substring(4,6);
var d=str.substring(6,8);
var date=new Date(y,parseInt(m,10)-1,d);
if(date.getDate()==parseInt(d,10)&&date.getMonth()==(parseInt(m,10)-1)&&date.getFullYear()==parseInt(y)){
  相关解决方案