Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: Compare 2 Dates

  1. #1
    New Coder
    Join Date
    Sep 2002
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Compare 2 Dates

    A HTML Form has 2 sets of 3 dropdown select lists. The 1st set is for users to enter the start time & the 2nd set is for users to enter the end time. The 1st dropdown select list in both the sets is where users will select hours, the 2nd dropdown select list in both the sets is where users will select minutes (the 2nd dropdown select list in both the sets have 4 options - 00, 15, 30 & 45). The 3rd dropdown select list in both the sets is where users will select either AM or PM.

    Now it's quite obvious that the start time can come after the end time (on the same date). For e.g. you can't let users select the start time as 9:00 AM & the end time as 6:00 AM.

    How do I validate that the end time always comes after the start time & vice-versa?

    Thanks,

    Arpan

  • #2
    New Coder
    Join Date
    Sep 2002
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts
    OK...friends...I got the solution but am facing a small problem. his is how I did it :
    Code:
    funcion checkTime(){
        var dtDate=new Date()
        var iDate=dtDate.getDate()
        var iMonth=dtDate.getMonth()
        var iYear=dtDate.getYear()
    	
        var sHour=document.forms[0].sHour.options[document.forms[0].sHour.selectedIndex].value;
        var sMinute=document.forms[0].sMinute.options[document.forms[0].sMinute.selectedIndex].value;
        var sSeconds="00";
        var eHour=document.forms[0].eHour.options[document.forms[0].eHour.selectedIndex].value;
        var eMinute=document.forms[0].eMinute.options[document.forms[0].eMinute.selectedIndex].value;
        var eSeconds="00";
    	
        var starttime=new Date(iDate,iMonth,iYear,sHour,sMinute,sSeconds);
        var endtime=new Date(iDate,iMonth,iYear,eHour,eMinute,eSeconds);
    	
        if(endtime<starttime){
            alert("END TIME CANNOT COME BEFORE START TIME!")
            document.forms[0].eHour.focus()
            return false
        }
        else if(starttime==endtime){
            alert("END TIME CANNOT BE THE SAME AS START TIME!")
            document.forms[0].eHour.focus()
            return false
        }
    }
    The above code works fine but have a look at the else if line. If both the start time & end time are, say, 7:45 AM, then the else if condition doesn't get executed & the alert message doesn't pop up!

    Any idea what could be the reason behind that?

    Thanks,

    Arpan

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    The Date constructor syntax is:

    Code:
    new Date(yr_num, mo_num, day_num [, hr_num, min_num, sec_num, ms_num])
    What you used is:

    Code:
    new Date(day_num, mo_num, yr_num [, hr_num, min_num, sec_num, ms_num])
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •