Compare Dates in Javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • emm
    New Member
    • Nov 2006
    • 8

    Compare Dates in Javascript

    Hi to everybody! I want to compare current date (server date) to the date inputted by the user. It should detect if inputted date is earlier than or equal to the current date.
    For example:
    Current date is 3/6/3008
    Inputted Date by the user is 03/05/2008


    Below is my function:

    Code:
    function checkDateInput(dateString){
                   var error="";
    	with(currDate){
    		currDate = getMonth()+1+"/"+getDate()+"/"+getFullYear();
    	}
    	
    	if(dateString != ""){
    		if(currDate >= dateString){
    			error = "Publication Date must not earlier than or equal to current date.";
    			return error;
    		}
    	}
    	return error;
    }
    Please help me. Thanks in advance and More Power!
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Use the Date() object to compare dates, see for example, this link.

    Comment

    • rnd me
      Recognized Expert Contributor
      • Jun 2007
      • 427

      #3
      Originally posted by acoder
      Use the Date() object to compare dates, see for example, this link.
      that wont validate by the server date, it uses the user's system's date, which can be more easily forged.

      you will need to use a very simple xmlHtttpRequest to grab the server date.

      would you like that code?

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Originally posted by rnd me
        that wont validate by the server date, it uses the user's system's date, which can be more easily forged.
        That's right, but I did mean the JavaScript Date() object being set from the server whilst the page was loading, though an XMLHttp request would perhaps be more accurate (if the user happened to be on the page for more than one day!).

        Comment

        • rnd me
          Recognized Expert Contributor
          • Jun 2007
          • 427

          #5
          Originally posted by acoder
          That's right, but I did mean the JavaScript Date() object being set from the server whilst the page was loading, though an XMLHttp request would perhaps be more accurate (if the user happened to be on the page for more than one day!).
          how would the Date object be affected by the server date?
          i don't see how it has anything to do with page visit length, though i could be mis-understanding something.

          if my clock is off, due to hacking or a bad CMOS battery for instance, the Date object will reflect that incorrect date.

          this code gets a valid date object using the server's date:

          Code:
                  function serverDate() {
                      X = !window.XMLHttpRequest ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest;
                      X.open("GET", window.location, false);
                      X.send("");
                      return Date(X.getResponseHeader("Date"));
                  }

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Originally posted by rnd me
            how would the Date object be affected by the server date?
            i don't see how it has anything to do with page visit length, though i could be mis-understanding something.

            if my clock is off, due to hacking or a bad CMOS battery for instance, the Date object will reflect that incorrect date.
            I know that. I meant setting the Date object using server-side code, either using the Date() constructor or the set*** methods. If the user was for some reason still on the same page the next day, the date would now be off by one day (very unlikely, of course, but all the same...)

            Comment

            Working...