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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Sep 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Checking for a future date problem

    I have a problem checking that a date is in the past or future.

    Everything seems fine but the alert always comes back saying the date is in the past.



    Code:
    function OpenTimesheet(TSdate)
    {
    var SelectedDate = new Date(TSdate)
    
    	if (SelectedDate > Date())
    	{
    	alert (SelectedDate+' is a AFTER '+Date())
    	}
    	else
    	{
    	alert (SelectedDate+' is a BEFORE '+Date())
    	}
    }

    TSdate is in yyyy/mm/dd format and looks fine, in both alerts the dates are written correctly, it just always says they are a 'BEFORE' date regardless.

    Any help with this one would be greatly apprieciated.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Try this:-

    Code:
    <script type = "text/javascript">
    
    function checkFutureDate(end_year, end_month, end_day) {
    end_month = end_month -1;  // months in Javascript are 0-11
    var now = new Date().getTime();
    var d = new Date();
    d.setFullYear(end_year, end_month, end_day);  // YYYY,MM(0-11),DD
    var selectedDate = d.getTime();  // today or after
    if (selectedDate <= now) {  // valid after today's date
    //if (selectedDate < now) {  // valid on today's date or after
    alert ("Date must be (on or) after today's date!");
    return false;
    }
    alert ("Future date is valid");
    return true;
    }
    
    checkFutureDate(2010,09,15);  // 12th September 2010
    
    </script>

    "It is inaccurate to say that I hate everything. I am strongly in favor of common sense, common honesty, and common decency. This makes me forever ineligible for public office". H. L. Mencken - US editor (1880 - 1956)

  • Users who have thanked Philip M for this post:

    gdalldan (09-13-2010)

  • #3
    New to the CF scene
    Join Date
    Sep 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    worked perfectly thanks, could you shed any light on what was wrong to start with?

    Obviosuly splitting the date into yyyy,mm,dd has something to do with it and SetFullYear also must be doing something essential.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    TSdate must be broken down into year,month,day.

    Remember that in Javascript Months are 0-11.

    Code:
    function OpenTimesheet(yy,mm,dd){
    
    var SelectedDate = new Date(yy,mm-1,dd);
    var today = new Date();
    if (SelectedDate > today)	{
    alert (SelectedDate+' is a AFTER '+Date());
    }
    else {
    alert (SelectedDate+' is a BEFORE '+Date());
    }
    }
    
    OpenTimesheet(2010,09,12);  // 12th September 2010


  •  

    Posting Permissions

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