how to instantiate a date object with UK format date??

Discussion in 'Javascript' started by amith, Feb 11, 2004.

  1. amith

    amith Guest

    hi

    I have written javascript for comparing two dates in US format and
    finding out whether the start date is greater than the end date and
    vice versa.

    In this attempt i have instantiated the date object with the date
    string which the user inputs from a pop up calendar.

    somewhat like this..........

    var startdate= new Date(document.MForm.startDate.value);

    Where MForm = form name
    startDate= The name of the TextField where i fetch the date
    from the user
    in the US format(MM/DD/YYYY).

    All my compare logic is based on the US format dates, which is
    somewhat like this..........

    var startdate= new Date(document.MForm.startDate.value);
    var enddate=new Date(document.MForm.endDate.value);
    var today=new Date();
    var one_day=1000*60*60*24;
    var end_start_diff =
    Math.ceil((enddate.getTime()-startdate.getTime())/(one_day));

    if((end_start_diff < 0))
    {
    alert(" End date should be greater than Start date");
    document.MForm.endDate.focus();
    return(false);
    }

    this logic works fine with US dates. But i realised that the same
    code would not work if I accpet UK format dates from the
    user.......b'cos

    new Date("02/11/2004") US Format (MM/DD/YYYY) gives Feb 11,
    2004
    but... new Date("11/02/2004") UK Format (DD/MM/YYYY) gives Nov 2,
    2004

    ......and this is what is causing the problem.

    The soln what i have thought is to change the UK format date to US
    format in the date.
    But i need to know if a Date Object in javascript can be instantiated
    with the UK format of date as the parameter??

    ex: new Date("11/02/2004") should gimme Feb 11, 2004. Is there any
    method i can do this. If not what are the other easy methods to solve
    this problem??

    Thanks in advnace for helping.

    Regards

    Amith
     
    amith, Feb 11, 2004
    #1
    1. Advertising

  2. On 11 Feb 2004 05:47:58 -0800, amith <> wrote:

    > The soln what i have thought is to change the UK format date to US
    > format in the date.
    > But i need to know if a Date Object in javascript can be instantiated
    > with the UK format of date as the parameter??
    >
    > ex: new Date("11/02/2004") should gimme Feb 11, 2004. Is there any
    > method i can do this. If not what are the other easy methods to solve
    > this problem??


    If you site is international, it would be a wise decision to use the
    international date format, yyyy-mm-dd. There can be little confusion here.
    Alternatively, you can split the one field into three, marking each
    clearly as day, month, and year.

    Dr Stockton, a regular poster here, has produced comprehensive information
    on date and time manipulation. One of the pages, covering input and
    validation, can be found here:

    http://www.merlyn.demon.co.uk/js-date4.htm

    Mike


    Dr Stockton's JavaScript pages:

    http://www.merlyn.demon.co.uk/js-index.htm

    --
    Michael Winter
    d (replace ".invalid" with ".uk" to reply)
     
    Michael Winter, Feb 11, 2004
    #2
    1. Advertising

  3. JRS: In article <>, seen in
    news:comp.lang.javascript, Michael Winter <
    d> posted at Wed, 11 Feb 2004 14:08:33 :-

    >> ex: new Date("11/02/2004") should gimme Feb 11, 2004. Is there any
    >> method i can do this. If not what are the other easy methods to solve
    >> this problem??

    >
    >If you site is international, it would be a wise decision to use the
    >international date format, yyyy-mm-dd. There can be little confusion here.


    Since new Date("2004-02-11") -> NaN
    but new Date("2004/02/11") -> Date Object holding start of
    local today
    ISTM that, if the date is not to be validated, the latter form is
    better.

    If it is to be validated, one can easily allow [-] [/] [-/] [\.] etc. as
    separators, and can also require that the second separator matches the
    first.


    In the code shown, the OP only needs
    if (enddate<=startdate) <complain>
    He seems inconsistent as respects the effect of having equal dates and
    the wording of the complaint.

    --
    © 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, Feb 11, 2004
    #3
    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. Brian Candy
    Replies:
    2
    Views:
    1,174
    Janaka
    Feb 18, 2004
  2. Ray5531
    Replies:
    14
    Views:
    637
    Ray5531
    May 5, 2005
  3. J-T
    Replies:
    7
    Views:
    423
  4. =?Utf-8?B?QW5kcmV3?=

    How to instantiate a user defined object in page_load()

    =?Utf-8?B?QW5kcmV3?=, Aug 24, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    424
    =?Utf-8?B?VmlqYXlha3Jpc2huYQ==?=
    Aug 24, 2005
  5. Replies:
    3
    Views:
    12,576
    Alex Hunsley
    Mar 10, 2006
Loading...

Share This Page