www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 21

Thread: compare dates in Javascript - Urgent

  1. #1
    Join Date
    Mar 2005
    Posts
    3

    Exclamation compare dates in Javascript - Urgent

    Hi,

    I need to compare 2 dates in a FORM which entered by users using a calender picker.

    Rule: To check if date2 is < date1+28 days.

    Questions:
    How do I add 28 days to the field date1?
    How Can I compare them?

    Regards

  2. #2
    Join Date
    Jun 2003
    Location
    here
    Posts
    4,551
    what format are you dates in?
    If you are using PHP please use the [PHP] and [/PHP] forum tags for highlighting...
    The same applies to HTML and the forums [HTML][/HTML] tags.

  3. #3
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428

  4. #4
    Join Date
    Mar 2005
    Posts
    3
    UK format - dd/mm/yyyy

  5. #5
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,270
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
     "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta name="Content-Script-Type" content="text/javascript">
    <meta name="Content-Style-Type" content="text/css">
    <title>Example</title>
    
    <script type="text/javascript">
    <!--
    function check (f) {
    	var start = new Date (f.start.value);
    	var end = new Date (f.end.value);
    	
    	start.setDate (start.getDate() + 28);
    	if (end >= start) {alert (); return false}
    	}
    // -->
    </script>
    
    <style type="text/css">
    <!-- 
    fieldset {padding:1ex; width:10em} 
    label {display:block; margin:1em 0}
    input {display:block}
    button {display:block; margin:auto}
    -->
    </style>
    
    </head>
    <body>
    <form action="some-script.pl" onsubmit="return check (this)">
    <fieldset>
    <legend>Dates</legend>
    <label>Start Date<input name="start" onchange="this.value = new Date (this.value).toDateString()" type="text"></label>
    <label>End Date<input name="end" onchange="this.value = new Date (this.value).toDateString()" type="text"></label>
    <button type="submit">Submit</button>
    </fieldset>
    </form>
    </body>
    </html>
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  6. #6
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,270
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
     "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta name="Content-Script-Type" content="text/javascript">
    <meta name="Content-Style-Type" content="text/css">
    <title>Example</title>
    
    <script type="text/javascript">
    <!--
    Date.prototype.toDDMMYYYYString = function () {return isNaN (this) ? 'NaN' : [this.getDate() > 9 ? this.getDate() : '0' + this.getDate(), this.getMonth() > 8 ? this.getMonth() + 1 : '0' +  (this.getMonth() + 1), this.getFullYear()].join('/')}
    
    Date.fromDDMMYYYY = function (s) {return (/^(\d\d?)\D(\d\d?)\D(\d{4})$/).test(s) ? new Date(RegExp.$3, RegExp.$2 - 1, RegExp.$1) : new Date (s)}
    
    function check (f) {
    	var start = Date.fromDDMMYYYY (f.start.value);
    	var end = Date.fromDDMMYYYY (f.end.value);
    	
    	start.setDate (start.getDate() + 28);
    	if (end >= start) {alert (); return false}
    	}
    // -->
    </script>
    
    <style type="text/css">
    <!-- 
    fieldset {padding:1ex; width:10em} 
    label {display:block; margin:1em 0}
    input {display:block}
    button {display:block; margin:auto}
    -->
    </style>
    
    </head>
    <body>
    <form action="some-script.pl" onsubmit="return check (this)">
    <fieldset>
    <legend>Dates</legend>
    <label>Start Date<input name="start" onchange="this.value = Date.fromDDMMYYYY (this.value).toDDMMYYYYString()" type="text"></label>
    <label>End Date<input name="end" onchange="this.value = Date.fromDDMMYYYY (this.value).toDDMMYYYYString()" type="text"></label>
    <button type="submit">Submit</button>
    </fieldset>
    </form>
    </body>
    </html>
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  7. #7
    Join Date
    Jun 2003
    Location
    here
    Posts
    4,551
    you acidentaly posted twice charles.
    If you are using PHP please use the [PHP] and [/PHP] forum tags for highlighting...
    The same applies to HTML and the forums [HTML][/HTML] tags.

  8. #8
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Nope. In the second he solved your date format dd/mm/yyyy

  9. #9
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,270
    Quote Originally Posted by Kor
    Nope. In the second he solved your date format dd/mm/yyyy
    Actually, the second one will take most any date format.

    The native Date constructor is supposed to work with multiple formats, but just which ones is not specified. Implimentation may be locale dependent. In the US dd/mm/yyyy isn't supported to make room for our mm/dd/yyyy. Try the first one in England, just for kicks, but the second one will always work with that format. If you feed it another format then it defers to the native constructor.

    Try my second one with a few other formats and you'll see another feature.
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  10. #10
    Join Date
    Mar 2005
    Posts
    3
    Thanks guy. but it doesn't work I've put in 31/03/2005 for both start and end date but when displaying it I get "Wed Aug 1 00:00:00 UTC +0100 2007" and "Tue Jul 3 00:00:00 UTC +0100 2007"

    Any idea? Do I need to convert the dates before I compare them?

  11. #11
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,270
    It would seem that you haven't properly copied my example.
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  12. #12
    Join Date
    Sep 2004
    Location
    Midwest
    Posts
    1,629
    <HTML>
    <Head>
    <Script Language=JavaScript>

    function calcJulian(isDate){

    gregDate = new Date(isDate);
    year = gregDate.getFullYear();
    month = gregDate.getMonth()+1;
    day = gregDate.getDate();
    A = Math.floor((7*(year+Math.floor((month+9)/12)))/4);
    B = day+Math.floor((275*month)/9)
    isJulian = (367*year)-A+B+1721014;
    return isJulian;
    }

    function validate(date1,date2){

    tmp = date1.split("/")
    xDate = tmp[1]+"/"+tmp[0]+"/"+tmp[2];
    refDate = calcJulian(xDate);
    tmp = date2.split("/")
    xDate = tmp[1]+"/"+tmp[0]+"/"+tmp[2];
    fwdDate = calcJulian(xDate);

    if (fwdDate-refDate > 28 || fwdDate <= refDate)
    {
    alert('Date2 Must not be more than 28 days later than Date1');
    document.forms.Form1.nDate2.value = "";
    document.forms.Form1.nDate2.focus();
    }
    }

    </Script>
    </Head>
    <Body>
    <Form name='Form1'>
    Date 1 (dd/mm/yyyy): <input type=text size=9 name='nDate1' ><br>
    Date 2 (dd/mm/yyyy): <input type=text size=9 name='nDate2' onblur="validate(nDate1.value,nDate2.value)"><br>
    Nothing: <input type=text size=9>
    </Form>
    </Body>
    </HTML>
    Last edited by Warren86; 04-01-2005 at 04:25 PM.

  13. #13
    Join Date
    Apr 2005
    Posts
    7
    Hey,

    I've been stuck with this date problem for a while. I have one date in the format: month,day eg for April 4th it's: April,4

    and the other date is in the format: year,month,day

    i cant get the year for the first date, but i want to compare the dates. I want to know if the first date is greater than or equal to the second date

    PLEASE help me!!!!

    Thanks

  14. #14
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,270
    Code:
    <script type="text/javascript">
    <!--
    alert (new Date ('April, 4 2005') >= new Date ('2005, May, 9'))
    // -->
    </script>
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  15. #15
    Join Date
    Feb 2005
    Posts
    61
    Quote Originally Posted by Charles
    Actually, the second one will take most any date format.

    The native Date constructor is supposed to work with multiple formats, but just which ones is not specified. Implimentation may be locale dependent. In the US dd/mm/yyyy isn't supported to make room for our mm/dd/yyyy. Try the first one in England, just for kicks, but the second one will always work with that format. If you feed it another format then it defers to the native constructor.

    Try my second one with a few other formats and you'll see another feature.
    IT trys to make a MEANINGFUL date out of it, the exact accepted format for in put is as stated here in the www.msdn.com site http://msdn.microsoft.com/library/de...6jsobjdate.asp

    JScript

    Date Object
    Enables basic storage and retrieval of dates and times.

    dateObj = new Date()
    dateObj = new Date(dateVal)
    dateObj = new Date(year, month, date[, hours[, minutes[, seconds[,ms]]]])

    Arguments

    dateObj Required. The variable name to which the Date object is assigned.

    dateVal Required. If a numeric value, dateVal represents the number of milliseconds in Universal Coordinated Time between the specified date and midnight January 1, 1970. If a string, dateVal is parsed according to the rules in the parse method. The dateVal argument can also be a VT_DATE value as returned from some ActiveX® objects.
    Anything is possible, how much it will cost is another matter...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles