javascript中的日期时间比较不起作用

时间:2014-01-06 11:16:45

标签: javascript validation datetime

我想在表单中验证dateTime输入字段,如果没有输入则需要提供警告消息,如果“FromDate”(例如:17-12-2013 19:14:58)>比“CurrentDate”需要以“ToDate”>相同的方式提供警报信息。比“CurrentDate”它需要给mes而且如果“fromDate”>“ToDate”在那个cse中也需要给出一些警告信息我为它编写代码但是它不起作用可以任何人帮助我吗? 代码将是这样的:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>     
<%@ taglib uri="http://displaytag.sf.net" prefix="display" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
 <title>Preva Tracking Systems</title>
<link rel="icon" href="pictures/preva_logo.png" />
<link rel="stylesheet" href="css/screen.css" type="text/css">  
<link rel="stylesheet" href="css/site.css" type="text/css">  
<link href="css/sty.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/datetimepicker.js"></script>
<script type="text/javascript" src="js/overspeedvalidate.js" >
</script>
</head>
<body>
<form  name="myForm" onsubmit="return validateForm()" action="ReportGenerator?action=OverspeedEventDBReport" method="post">
<table width=100% border="1">
            <tr bgcolor=#075A99 >
            <td width=100% align=left><b><font color=white>OverSpeed Event Report</font></b></td>

            </tr>
            </table>



               <table cellpadding="0" cellspacing="0" width="100%" class="reportSearchBorder">
                  <tr><td>&nbsp;</td></tr>
                   <tr>
                       <td width="100%">
                                    <table width="100%" cellpadding="0" cellspacing="0" border=0>           
                                      <tr>
                                          <td  align="left" id="VehicleIdDescDiv" valign="middle">Vehicle Number</td>
                                             <td >
                                                    <table align=left border=0>
                                                       <tr>
                                                        <td  align=left>
                                                            <select name="vehicleId"  class="txtbig" ><option value="">Select vehicle number</option>
                                                              <c:forEach var="vehiclelist" items="${devicelists}">
                                                                <option value="${vehiclelist.deviceID}">${vehiclelist.description}</option>
                                                                </c:forEach></select>

                                                     </td>
                                                    </tr>
                                                    <tr><td align="center"></td></tr>
                                                        <tr>
                                                         <td valign="middle"></td>

                                                     </tr>

                                                  </table>
                                               </td><td  id="VehicleIdDescDiv" valign="middle">Show overspeed instances more than</td>
                                               <td >
                                                              <select name="speed"   class="txtbig" >
                                                                <option value="">Select</option>
                                                                <option value="&gt;100km">&gt;100km</option>
                                                                <option value="&gt;90km">&gt;90km</option>
                                                                <option value="&gt;80km">&gt;80km</option>
                                                                <option value="&gt;70km">&gt;70km</option>
                                                                <option value="&gt;60km">&gt;60km</option>
                                                                <option value="&gt;50km">&gt;50km</option>
                                                                <option value="&gt;40km">&gt;40km</option>
                                                                <option value="&gt;30km">&gt;30km</option>
                                                                <option value="&gt;20km">&gt;20km</option>
                                                                <option value="&gt;10km">&gt;10km</option>
                               </select>  </td>
                                  </tr></table>

                <table>

                             <tr><td>&nbsp;</td></tr>
                      <tr>
                           <td width="60%">

<table>


<tr><td>From Date</td>
    <td>
            <input type="Text" name="AnotherDate" id="demo1" maxlength="25" size="25"><a href="javascript:NewCal('demo1','ddMMyyyy',true,24)"><img src="pictures/cal.gif" width="16" height="16" border="0" alt="select"></a>
            <br>(eg:- 17-12-2013 19:14:58)
    </td>
        <td>To Date</td>
    <td>
            <input type="Text" id="demo2" name="ADate" maxlength="25" size="25"><a href="javascript:NewCal('demo2','ddMMyyyy',true,24)"><img src="pictures/cal.gif" width="16" height="16" border="0" alt="select"></a>
            <br>(eg:- 17-12-2013 19:14:58)
    </td> 

 </tr>
</table>




</td>
        </tr>
    <tr><td>&nbsp;</td></tr>
    <tr>
        <td width="60%"><table width="100%" cellpadding="0" cellspacing="0">
            <tr>

                <td align="right"><input type="submit" value="Show Report" class="button1"></td>

            </tr>
        </table></td>
    </tr>
    <tr><td>&nbsp;</td></tr>
</table>

            </td>   
            </tr>
        </table>                
    </form>
</body>
</html>

overspeedvalidate.js代码将是这样的:

function validateForm() {

    var todayDate = new Date();
    var fromDate=document.myForm.AnotherDate.value;
    var toDate=document.myForm.ADate.value;
    var fromparts = fromDate.split(" ");
    var fromdate= fromparts[0];
    var fromtime=fromparts[1];
    var fromdayfield = fromdate.split("-")[0];
    var frommonthfield = fromdate.split("-")[1];
    var fromyearfield = fromdate.split("-")[2];
    var fromhourfield = fromtime.split(":")[0];
    var fromminfield = fromtime.split(":")[1];
    var fromsecfield = fromtime.split(":")[2];


  var toparts = toDate.split(" ");
  var todate= toparts[0];
  var totime=toparts[1];
  var todayfield = todate.split("-")[0];
  var tomonthfield = todate.split("-")[1];
  var toyearfield = todate.split("-")[2];
  var tohourfield = totime.split(":")[0];
  var tominfield = totime.split(":")[1];
  var tosecfield = totime.split(":")[2];

  var fromDate = new Date(fromyearfield, frommonthfield-1, fromdayfield,fromhourfield,fromminfield,fromsecfield);
  var toDate = new Date(toyearfield, tomonthfield-1, todayfield,tohourfield,tominfield,tosecfield);

    if(document.myForm.vehicleId.selectedIndex ==0 ){
        alert("Please select vehicle number");
        document.myForm.vehicleId.focus();
        return false;
    }else if (document.myForm.speed.selectedIndex == 0){
        alert("Please select Speed");
        return false;
    }else if (document.myForm.AnotherDate.value == null || document.myForm.AnotherDate.value == ""){
        alert("Please select FromDate");
        document.myForm.AnotherDate.focus();
        return false;
    }else if (fromDate.getTime()>todayDate.getTime()){
        alert("Please select FromDate lessthan CurrentDate");
        document.myForm.AnotherDate.focus();
        return false;
    }
    else if (document.myForm.ADate.value == null || document.myForm.ADate.value == ""){
        alert("Please select ToDate");
        document.myForm.ADate.focus();
        return false;
    }else if(toDate.getTime()>todayDate.getTime()){
        alert("Please select ToDate lessthan CurrentDate");
        document.myForm.ADate.focus();
        return false;
    }else if (fromDate.getTime()>toDate.getTime()){
        alert("Please select FromDate lessthan ToDate");
        document.myForm.AnotherDate.focus();
        return false;
    }
    return true;
}

当我考虑到最新时它工作正常但是当我考虑日期和时间它不起作用可以解决我的问题

1 个答案:

答案 0 :(得分:0)

我尝试了一个javascript函数,就像下面的小提琴一样,它对我有用。 只需使用日期时间格式合成两个日期变量即可使用

http://jsfiddle.net/fNPvf/1307/

功能改变(){

   //            new Date(Year, Month, Date, Hr, Min, Sec);
   var dateOne = new Date(2012, 04, 20, 14, 55, 59);
   var dateTwo = new Date(2012, 04, 20, 12, 10, 20);
    //Note: 04 is month i.e. May
   if (dateOne > dateTwo) {
        alert("Date One is greather then Date Two.");
    }else {
        alert("Date Two is greather then Date One.");
    }

}