js 日期比较

参考一:
function checkDate(){
   var startTime=document.getElementById("startTime").value; //获得系统日期的文本值
   var endTime=document.getElementById("endTime").value; //获得用户选择的日期文本值
   var aStart=startTime.split('-'); //转成成数组,分别为年,月,日,下同
   var aEnd=endTime.split('-');
   var startDate = aStart[0]+"/" + aStart[1]+ "/" + aStart[2];
   var endDate = aEnd[0] + "/" + aEnd[1] + "/" + aEnd[2];
   if (startDate > endDate) {
    $("#moneyMessage").html("<font color=red>开始日期必须小于结束日期</font>");
    return false;
   }
    return true;
  }

参考二:
<SCRIPT LANGUAGE="JavaScript">
<!--
function deal(){
//var Y = 6;
//var R ="2009-11-01";
var R = document.getElementById('date').value +'';
var Y = document.getElementById('month').value;

alert(R);
var year=R.substr(0,4);
var month=R.substr(5,2);
var day=R.substr(8,2);

if((parseInt(month)+parseInt(Y)) > 12){

year++;
month = parseInt(month) + parseInt(Y) - 12;
}else{
month = parseInt(month) + parseInt(Y);
}

alert(year+"-"+month+"-"+day);

R = new Date(year,month,day,00,00,00);
dateNow = new Date();

var diff = R.getTime() - dateNow.getTime()
var days = Math.floor(diff/(1000*60*60*24));
var str = "与今天相差" + days + "天"

if(R > dateNow){
alert("大于今天!"+"\n"+str);
}else{
alert("小于今天!"+"\n"+str);
}
}
//-->
</SCRIPT>

月<input type="text" id="month" value="6">
日期 <input type="text" id="date" value="2009-11-01">
<input type="button" value="submit" onClick="deal()">

参考三:


JS 日期比较默认分类 2009-09-21 15:00:12 阅读135 评论0   字号:大中小 订阅 .

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>
<script language="javascript">
function dateDifferent()
{
var time1 = document.form1.time1.value;
var time2 = document.form1.time2.value;
var date1 = new Date(time1);
var date2 = new Date(time2);
//var DiffDate = DateDiff("d",time1,time2);

//long begintime = begindate.gettime();
//long endtime2 = enddate.gettime();
//long betweendays = (long)((endtime - begintime) / (1000 * 60 * 60 *24) + 0.5);


//var begintime1 = time1.gettime();
//var endtime2 = time2.gettime();
//var betweendays = ((endtime2-begintime1)/(1000*60*60*24)+0.5);
//alert(betweendays);
var resultDate = DateDiff(time1,time2);
alert(DateDiff(time2,time1));
if(resultDate>0){
  alert("开始日期大于结束日期!");
}else{
  alert("开始日期小于结束日期!");
}

}
//比较两个日期相差的天数的函数
function  DateDiff(s1,  s2){   
//s1和s2都是2007-8-10格式
    s1 = s1.replace(/-/g, "/");
    s2 = s2.replace(/-/g, "/");
       s1 = new Date(s1);
       s2 = new Date(s2);

       iDays  =  parseInt((s1-  s2)  /  (1000 * 60 * 60 * 24));
       return iDays;
}


  /*var days= s1.getTime() - s2.getTime();
  var time = parseInt(days / (1000 * 60 * 60 * 24));
  alert("相差天数: " + days);*/

//将日期增加10天的函数
function cc()
{
//可以加上错误处理
alert("2003-08-02".replace(/-/g,   "/"));
var   s   =   "2003-08-02".replace(/-/g,   "/");  
var   d   =   new   Date(s);  
//在js中将时间加10天
d.setTime(d.getTime()  +  10*24*60*60*1000);     //单位是毫秒  
alert(d)
//转换为2003-08-02格式
var descDate = d.getFullYear()+"-"+ ((d.getMonth()+1)>10?(d.getMonth()+1):("0"+(d.getMonth()+1)))   
       +"-"+(d.getDate()>10?d.getDate():("0"+d.getDate()));
alert(descDate);
}


//将当前日期转换成2003-08-02格式
function checkDate (){
var date = new Date();
var nowDate = date.getFullYear()+"-"+ ((date.getMonth()+1)>10?(date.getMonth()+1):("0"+(date.getMonth()+1)))   
       +"-"+(date.getDate()>10?date.getDate():("0"+date.getDate()));
alert(nowDate);
}
</script>

<body>
<form name="form1" method="post" action="">
  <p>开始日期 :
    <input type="text" name="time1">
    <input type="button" name="Submit2" value="增加10天" onClick="cc();">
</p>
  <p>结束日期:
    <input type="text" name="time2">
</p>

  <p><input type="button" name="Submit" value="比较" onClick="return dateDifferent()">
  </p>
</form>
</body>
</html>
参考四:
function CompareDate(inputDate){
     var currentDate = new Date();
     if(currentDate.getYear() > inputDate.getYear())
       return -1;
     else if(currentDate.getYear() < inputDate.getYear())
       return 1;
    
     if(currentDate.getMonth() > inputDate.getMonth())
       return -1;
     else if(currentDate.getMonth() < inputDate.getMonth())
       return 1;
    
     if(currentDate.getDate() > inputDate.getDate())
       return -1;
     else if(currentDate.getDate() < inputDate.getDate())
       return 1;
    
     return 0;
    }
  
    function Compare() {
    var sday = document.getElementById("day").value.split('-');
     var inputDate = new Date(sday[0],sday[1]-1,sday[2]);
     if(isNaN(inputDate)){
       alert("不是日期");
    return;
     }
     var result = CompareDate(inputDate);
     if(result < 0)
       alert(inputDate.toLocaleString() + "小于当前日期");
     else if(result > 0)
       alert(inputDate.toLocaleString() + "大于当前日期");
     else
       alert(inputDate.toLocaleString() + "等于当前日期");
    }
参考五:
JavaScript代码
  
function dateCompare(startdate,enddate)   
{   
var arr=startdate.split("-");    
var starttime=new Date(arr[0],arr[1],arr[2]);    
var starttimes=starttime.getTime();   
  
var arrs=enddate.split("-");    
var lktime=new Date(arrs[0],arrs[1],arrs[2]);    
var lktimes=lktime.getTime();   
  
if(starttimes>=lktimes)    
{   
return false;   
}   
else  
return true;   
  
}  


第二种:

JavaScript代码
//验证是否合法日期   
function CheckDate(strDate){   
      if(strDate.length>0){   
     var reg= /^(\d )-(\d{1,2})-(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;      
       if(!reg.test(strDate)){      
        return false;    
       }   
     }   
     return true;   
    }   
  
    
  
function isStartEndDate(startDate,endDate){   
     //alert(startDate "===" endDate);   
     if(startDate.length>0&&endDate.length>0){   
      var startDateTemp = startDate.split(" ");   
      var endDateTemp = endDate.split(" ");   
      var arrStartDate = startDateTemp[0].split("-");   
      var arrEndDate = endDateTemp[0].split("-");   
      var arrStartTime = startDateTemp[1].split(":");   
      var arrEndTime = endDateTemp[1].split(":");   
      var allStartDate = new Date(arrStartDate[0],arrStartDate[1],arrStartDate[2],arrStartTime[0],arrStartTime[1],arrStartTime[2]);   
      var allEndDate = new Date(arrEndDate[0],arrEndDate[1],arrEndDate[2],arrEndTime[0],arrEndTime[1],arrEndTime[2]);   
      if(allStartDate.getTime()>allEndDate.getTime()){   
       return false;   
      }   
     }   
     return true;   
    }   
  
    
  
function checkAskDate(StartDate,EndDate){   
     if(!CheckDate(StartDate.value)){   
      alert("开始日期格式不正确!\n正确格式为:2004-01-01 00:00");   
      StartDate.focus();   
      return false;   
     }   
     if(!CheckDate(EndDate.value)){   
      alert("结束日期格式不正确!\n正确格式为:2004-01-01 00:00");   
      EndDate.focus();   
      return false;   
     }   
     if(!isStartEndDate(StartDate.value,EndDate.value)){   
      alert("开始日期不能大于结束日期");   
      return false;   
     }   
     return true;   
    }   
  
    
  
============================================================================   
  
日期格式:XXXX年-XX月-XX日   
  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">   
<HTML>   
<HEAD>   
<TITLE> New document.</TITLE>   
<SCRIPT LANGUAGE="javascript">   
   function judgeDate()   
   {   
    var date1 = document.getElementById("dt1").value.replace(/-/g, "/");   
    var date2 = document.getElementById("dt2").value.replace(/-/g, "/");      
    var d1 = new Date(date1);   
    var d2 = new Date(date2);   
  
    if(Date.parse(d1) - Date.parse(d2)==0)   
    {   
     alert(date1 "等于" date2)   
    }   
    if(Date.parse(d1) - Date.parse(d2)<0)   
    {   
     alert(date1 "晚于" date2)   
    }   
    if(Date.parse(d1) - Date.parse(d2)>0)   
    {   
     alert(date1 "早于" date2)   
    }   
      
   }   
</SCRIPT>   
</HEAD>   
<BODY>   
<input id="dt1" value="2007-09-09" />   
<input id="dt2" value="2007-10-10"/>   
<input type="button" value="提交" onclick="judgeDate()"/>   
</BODY>   
</HTML>   
  
=============================================================   
  
<script language="javascript">   
function test(str1,str2)   
{   
var d1 = new Date(str1);   
var d2 = new Date(str2);   
if(Date.parse(d1) - Date.parse(d2)==0)   
{   
alert(str1 "等于" str2)   
}   
if(Date.parse(d1) - Date.parse(d2)<0)   
{   
alert(str1 "早于" str2)   
}   
if(Date.parse(d1) - Date.parse(d2)>0)   
{   
alert(str1 "晚于" str2)   
}   
}   
</script>   
<input type="button" value="确定" onclick="test('2005/4/12','2006/4/12')">  

你可能感兴趣的:(JavaScript,html)