开始日期必须早于结束日期

时间:2012-07-05 07:12:40

标签: javascript validation date

我正在比较一些下拉列表值,基本上开始日期必须早于结束日期

我通过护目镜和其他人发现了很多样本​​源代码,但它们只提供DD / MM / YYYY。
我只需要MM / YYY,当我试图删除“DD”时会出现错误,如果我是正确的,因为date关键字/元素。

有什么解决方案吗?任何帮助或参考链接都会很好。谢谢

P.s以下代码适用于DD / MM / YYYY

  

function y2k(number) {
    return (number < 1000) ? number + 1900 : number;
}
 function padout(number) {
    return (number < 10) ? '0' + number : number;
}

function validate(what) {
    var startday = what.startday.options[what.startday.selectedIndex].value;
    var startmonth = what.startmonth.options[what.startmonth.selectedIndex].value;
    var startyear = what.startyear.options[what.startyear.selectedIndex].text;
    var endday = what.endday.options[what.endday.selectedIndex].value;
    var endmonth = what.endmonth.options[what.endmonth.selectedIndex].value;

    unvalidstartdate = startday + '/' + startmonth + '/' + startyear;
    unvalidenddate = endday + '/' + endmonth;

    var startdate = new Date(startyear - 0, startmonth - 1, startday - 0);
    var enddate = new Date(endmonth - 1, endday - 0);

    var validstartdate = padout(startdate.getDate()) + '/'
            + padout(startdate.getMonth() + 1) + '/'
            + y2k(startdate.getYear())
    var validenddate = padout(enddate.getDate()) + '/'
            + padout(enddate.getMonth() + 1) + '/' + y2k(enddate.getYear());

    if (unvalidstartdate != validstartdate) {
        alert('Start Date: '
                + what.startday.options[what.startday.selectedIndex].text
                + ' '
                + what.startmonth.options[what.startmonth.selectedIndex].text
                + ' '
                + what.startyear.options[what.startyear.selectedIndex].text
                + ' is invalid');
        return false;
    }

    if (unvalidenddate != validenddate) {
        alert('End Date: '
                + what.endday.options[what.endday.selectedIndex].text + ' '
                + what.endmonth.options[what.endmonth.selectedIndex].text
                + ' '

                + ' is invalid');
        return false;
    }

    starttime = Date.UTC(y2k(startdate.getYear()), startdate.getMonth(),
            startdate.getDate(), 0, 0, 0);
    endtime = Date.UTC(y2k(enddate.getYear()), enddate.getMonth(), enddate
            .getDate(), 0, 0, 0);

    if (starttime < endtime) {
    alert('VALID');         
            // valid
        } else {
            alert('NOT VALID');
            return false;
    }

    currentdate = new Date();
    currenttime = Date.UTC(y2k(currentdate.getYear()), currentdate
            .getMonth(), currentdate.getDate(), 0, 0, 0);

    if (endtime < currenttime) {
        // valid
    } else {
        alert('End Date is not less than todays date');
        return false;
    }

    what.startdate.value = validstartdate;
    what.enddate.value = validenddate;
    return true;
}
 //-->
  </script>

 <form>
Start Date: <select name="startday">
    <option value="01">1st
    <option value="02">2nd
    <option value="03">3rd

</select> <select name="startmonth">
    <option value="01">january
    <option value="02">february
    <option value="03">march

</select> <select name="startyear">
    <option>1990
    <option>1991
    <option>1992
</select>

<p>

    End Date: <select name="endday">
        <option value="01">1st
        <option value="02">2nd
        <option value="03">3rd

    </select> <select name="endmonth">
        <option value="01">january
        <option value="02">february
        <option value="03">march

    </select>  <input type="hidden" name="startdate"> <input type="hidden"
        name="enddate">
   <p>

    <input type="button" onClick="validate(this.form)" value="Validate">
</form>

5 个答案:

答案 0 :(得分:0)

var startDateFinal = "your startdate element value";
var endDateFinal = "your enddate element value";
var startDate = new Date (startDateFinal);
var endDate = new Date(endDateFinal);
    if(endDate.getTime() > startDate.getTime()){
            // some logic
     }
     else{
           // some logic
     }

答案 1 :(得分:0)

var invalid=false;
var startDate="03/2011";//start date in mm/yyyy format
var endDate="04/2012";//end date in mm/yyyy format
var startMonth=startDate.substring(0,2);
var endMonth=endDate.substring(0,2);
var startYear=startDate.substring(3);
var endYear=endDate.substring(3);
if(startYear>endYear)
{
invalid=true;
}
else if(startYear<endYear && startMonth>endMonth)
invalid=true;

答案 2 :(得分:0)

Date constructor只需要年份和月份,您可以使用字符串,因此您可以直接使用表单控件中的值,如:

var start = new Date(startyear, --startmonth);
var end = new Date(endyear, --endmonth);

if (start < end) {
  // start is before end
}

哦,您应该使用getFullYear来获取全年数字。为了与Java兼容,原始getYear方法仅返回两位数年份。

答案 3 :(得分:0)

以下代码将为您提供两个日期字段,开始日期字段将小于结束日期字段。代码工作正常 您将在http://jquery.com/http://jquery.com/网站上获得jquery,jqueryui / calendar。

在HEAD部分中包含以下代码 包括jquery-1.5.1.min.js,jquery-ui-1.8.13.custom.min.js,jquery-ui-1.8.11.custom.css“,calendar.css”files

$(function() {
    var dates = $( "#txt_startdate, #txt_enddate" ).datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        changeYear: true,
        numberOfMonths: 2,
        dateFormat: 'yy-mm-dd',
        onSelect: function( selectedDate ) {
            var option = this.id == "txt_startdate" ? "minDate" : "maxDate",
                instance = $( this ).data( "datepicker" ),
                date = $.datepicker.parseDate(
                    instance.settings.dateFormat ||
                    $.datepicker._defaults.dateFormat,
                    selectedDate, instance.settings );
            dates.not( this ).datepicker( "option", option, date );
        }
    });


});

在html

的正文部分输入以下代码
<input name="txt_startdate" id="txt_startdate"  type="text" />
<input name="txt_enddate"  id="txt_enddate" type="text" />

希望这有助于你

答案 4 :(得分:0)

试试这个

function test()
{
var date1,date;
date1 = new Date(2011,11,1); 
// startdate drop down values   
//2011-->year   11--> startmonth  1-->startdate(this you keep always as 1 for startdate and end date) since you only wants to compare mm/yyyy
date2 = new Date(2002,11,1); 
// enddate drop down values   
//2011-->year   11--> endmonth  1-->enddate(this you keep always as 1 for startdate and end date) since you only wants to compare mm/yyyy

if(date1>date2)
    alert("date1 is > date2")
else
    alert("date2 is > date1")
}

check this date