compare dates.

Discussion in 'Javascript' started by alexis, Nov 30, 2003.

  1. alexis

    alexis Guest

    Hi,

    In a form I have the curent date
    <input name="datetoday" type="hidden" value="<? echo date("d/m/Y"); ?>">
    and
    <input type=text name="datebox" size=15>
    The date format is d/m/Y (day/month/year) in both case

    Now my problem is that i want to know if 'datebox' is valid, that means
    after 'datetoday'.

    I tried :

    function checkFields() {
    missinginfo = "";
    if (document.form.datebox.value < document.form.datetoday.value) {
    missinginfo += "\n - date not valid";
    }

    But this doesn't work because of the '/'.

    Can someone help me, thanks,

    AL
     
    alexis, Nov 30, 2003
    #1
    1. Advertising

  2. alexis <> writes:

    > In a form I have the curent date


    > The date format is d/m/Y (day/month/year) in both case


    I'll assume you'll have checked this, so we can trust the format.
    You should consider that that date format is ambiguous, since people
    in some coutries will read it as m/d/Y if they can.

    > Now my problem is that i want to know if 'datebox' is valid, that
    > means after 'datetoday'.


    > I tried :


    > if (document.form.datebox.value < document.form.datetoday.value) {


    You are comparing them as strings.

    > But this doesn't work because of the '/'.


    That's not the problem. If the format was YYYY/mm/dd, then string
    comparison would work, because later dates would compare larger as
    strings. It doesn't work for you, because you have the least
    significant part of the date first, so "10/01/1970" is larger than
    "09/12/2036", because "1" is larger than "0" and the rest is ignored.

    To use string comparison, you *must* use the same number of digits
    for each part every time, so the 3rd will be "/03", not "/3".

    > Can someone help me, thanks,


    Either change the format, or parse the string.

    Start with:
    ---
    var today = document.forms['form'].elements['datetoday'].value;
    var someday = document.forms['form'].elements['datebox'].value;
    var todayParts = today.split("/");
    var somedayParts = someday.split("/");
    ---
    That gives you arrays where, e.g., "31/11/2003" becomes
    ["31","11","2003"].

    You can now compare yourself or use the Date constructor:

    ---
    if ( (Number(todayParts[2]) < Number(somedayParts[2])) ||
    (Number(todayParts[2]) == Number(somedayParts[2]) &&
    ( (Number(todayParts[1]) < Number(somedayParts[1])) ||
    ( (Number(todayParts[1]) == Number(somedayParts[1])) &&
    (Number(todayParts[0]) < Number(somedayParts[0])) ))))
    {
    // today is before someday.
    }
    ---
    or
    --
    var todayDate = Date.UTC(Number(todayParts[2]), //year
    Number(todayParts[1])-1, //month
    Number(todayParts[0]) //date
    );
    var somedayDate = Date.UTC(Number(somedayParts[2]), //year
    Number(somedayParts[1])-1, //month
    Number(somedayParts[0]) //date
    );

    if ( todayDate < somedayDate) {
    // today < someday
    }
    ---

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Nov 30, 2003
    #2
    1. Advertising

  3. On Sun, 30 Nov 2003 12:43:13 +0100, alexis <> wrote:

    >Hi,
    >
    >In a form I have the curent date
    ><input name="datetoday" type="hidden" value="<? echo date("d/m/Y"); ?>">
    >and
    ><input type=text name="datebox" size=15>
    >The date format is d/m/Y (day/month/year) in both case
    >
    >Now my problem is that i want to know if 'datebox' is valid, that means
    >after 'datetoday'.
    >
    >I tried :
    >
    >function checkFields() {
    >missinginfo = "";
    >if (document.form.datebox.value < document.form.datetoday.value) {
    >missinginfo += "\n - date not valid";
    >}
    >
    >But this doesn't work because of the '/'.


    var d1 = document.form.datebox.value;
    var d2 = document.form.datetoday.value;
    if (Date.parse(d1) < Date.parse(d2))
    ...

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/js56jsmthparse.asp

    You don't need to verify that the string entered by the user is a
    valid date format before attempting to perform the comparison.

    Regards,
    Steve
     
    Steve van Dongen, Nov 30, 2003
    #3
  4. Steve van Dongen <> writes:

    >>The date format is d/m/Y (day/month/year) in both case


    > var d1 = document.form.datebox.value;
    > var d2 = document.form.datetoday.value;
    > if (Date.parse(d1) < Date.parse(d2))


    My browser's Date.parse parses "09/10/2003" as month/day/year, so it
    won't work.

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Nov 30, 2003
    #4
  5. JRS: In article <>, seen in
    news:comp.lang.javascript, Lasse Reichstein Nielsen <>
    posted at Sun, 30 Nov 2003 13:19:57 :-
    >alexis <> writes:
    >
    >> In a form I have the curent date

    >
    >> The date format is d/m/Y (day/month/year) in both case


    That, of course, is not in accordance with an applicable European Norm.
    Brussels is not always wrong.


    >I'll assume you'll have checked this, so we can trust the format.
    >You should consider that that date format is ambiguous, since people
    >in some coutries will read it as m/d/Y if they can.


    And, as you have subsequently shown, browsers in more countries will.


    >Either change the format, or parse the string.


    S = '23/04/03'
    S = S.replace(/(\d+).(\d+).(\d+)/, '$3/$2/$1')
    S = S.replace(/^(\d\d\/)/, '20$1')
    D = new Date(S)

    and compare Date Objects. Assumes D M Y, and D is d or dd, M is m or
    mm, Y is yy or yyyy.

    S = S.replace(/\b(\d)\b/g, '0$1') // inserts leading zeroes

    and with leading zero and corrected (to ISO) order & yyyy one can
    compare strings.

    OP : Test carefully. Better to get dates in proper yyyy-mm-dd form in
    the first instance.


    Mote that if the date is first split into Y M D strings one can generate
    a PseudoDate = (Y*100+ +M)*100+ +D and compare those directly; the
    extra + signs and spaces are *vital*. H'mmm - +D+(+M+Y*100)*100 seems
    OK too, & +D+100*(+M+100*Y) . PseudoDates can be compared but should
    not be subtracted, except to obtain the sign.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
    Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
    PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
    Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
     
    Dr John Stockton, Nov 30, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Lozzi

    Dates dates dates dates... SQL and ASP.NET

    David Lozzi, Sep 29, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    893
    Rob Schieber
    Sep 30, 2005
  2. PW

    Dates! Dates! Dates!

    PW, Aug 7, 2004, in forum: ASP General
    Replies:
    4
    Views:
    222
    Mark Schupp
    Aug 9, 2004
  3. Replies:
    1
    Views:
    226
    Jano Svitok
    Jul 17, 2007
  4. kellygreer1

    RFC-822 dates into Ruby dates

    kellygreer1, Jun 8, 2008, in forum: Ruby
    Replies:
    1
    Views:
    227
    Eric I.
    Jun 8, 2008
  5. Dr J R Stockton

    FAQ Dates; Opera Dates.

    Dr J R Stockton, Jun 18, 2007, in forum: Javascript
    Replies:
    0
    Views:
    177
    Dr J R Stockton
    Jun 18, 2007
Loading...

Share This Page