Form entry to Time part of database Date entry?

Discussion in 'Javascript' started by Noozer, Aug 1, 2005.

  1. Noozer

    Noozer Guest

    I have a textbox on one of my forms that is used to accept a time from the
    user. I need to write this value to a database to trigger an event later on.

    What I'd like to know is...

    Are there any functions in javascript that can convert different time
    formats to a known format (24 hour clock)... 5pm -> 17:00 or 5:00 to 05:00,
    etc. ???

    Assuming that I get the input into a 24 hour format, how can I create a full
    time/date variable with todays date and the users entered time?

    Finally, if the entered time is earlier than the current time, how can I
    create the above date/time combination using tomorrows date?

    Thanks!!!
     
    Noozer, Aug 1, 2005
    #1
    1. Advertising

  2. Noozer

    RobG Guest

    Noozer wrote:
    > I have a textbox on one of my forms that is used to accept a time from the
    > user. I need to write this value to a database to trigger an event later on.
    >
    > What I'd like to know is...
    >
    > Are there any functions in javascript that can convert different time
    > formats to a known format (24 hour clock)... 5pm -> 17:00 or 5:00 to 05:00,
    > etc. ???


    You need to parse whatever is input and feed it to your own date
    functions, probably all you need to know is here:

    <URL:http://www.merlyn.demon.co.uk/js-dates.htm>

    >
    > Assuming that I get the input into a 24 hour format, how can I create a full
    > time/date variable with todays date and the users entered time?


    A date object representing what the user's machine thinks is the
    current local time can be created with:

    var now = new Date();

    Adjusting the the time is shown below in a short example:

    <input type="text" name="aTime" onblur="
    var theTime = new Date();
    var t = this.value.split(':');
    theTime.setHours( t[0] );
    theTime.setMinutes( t[1] );
    theTime.setSeconds( 0 );
    alert( theTime );
    var now = new Date();
    if ( now > theTime ) {
    theTime.setDate( theTime.getDate() + 1 );
    }
    alert ( theTime );
    ">

    >
    > Finally, if the entered time is earlier than the current time, how can I
    > create the above date/time combination using tomorrows date?


    Yes, see above. But there are three big caveats:

    1. You must validate whatever the user enters to ensure it is
    something your functions will like, which means checking they entered
    properly formatted numbers and that they are within an acceptable
    range. For example, if you enter say 37:102 to the above example, it
    will quite happily interpret it as 14:42 tomorrow.

    2. The clock on the users machine may be wrong, so that 'now' is not
    today, or it may be sufficiently inaccurate as to cause spurious
    results for certain times.

    3. The user may not have javascript enabled/available.

    Over to you!

    --
    Rob
     
    RobG, Aug 1, 2005
    #2
    1. Advertising

  3. JRS: In article <w9lHe.80648$5V4.77990@pd7tw3no>, dated Mon, 1 Aug 2005
    08:35:40, seen in news:comp.lang.javascript, Noozer <>
    posted :

    >Are there any functions in javascript that can convert different time
    >formats to a known format (24 hour clock)... 5pm -> 17:00 or 5:00 to 05:00,
    >etc. ???


    Yes.

    >Assuming that I get the input into a 24 hour format, how can I create a full
    >time/date variable with todays date and the users entered time?


    with (D = new Date()) setHours(H, M, S)
    or with (D = new Date()) setUTCHours(H, M, S)

    where H M S are numbers or strings for hours minutes seconds.

    N.B. VERY ancient browsers may not allow multiple parameters in
    setHours().

    >Finally, if the entered time is earlier than the current time, how can I
    >create the above date/time combination using tomorrows date?


    Add
    if (D < new Date()) D.setDate(D.getDate()+1)
    or if (D < new Date()) D.setUTCDate(D.getUTCDate()+1)

    If there is any possibility of the actual time, or of H M S, being
    affected to Summer Time clock changes in Spring and Autumn, then test
    carefully that what happens is satisfactory.

    If efficiency matters, which is unlikely, do parameterless new Date()
    only once. That's also vital if the user may be working at local (or
    UTC) midnight.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Dr John Stockton, Aug 1, 2005
    #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. Chris Berg
    Replies:
    0
    Views:
    830
    Chris Berg
    Oct 27, 2003
  2. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,332
    Michael Borgwardt
    May 30, 2004
  3. Noozer
    Replies:
    2
    Views:
    1,164
    Noozer
    Aug 1, 2005
  4. AtomicBob
    Replies:
    14
    Views:
    954
    Toby Inkster
    May 2, 2006
  5. Keith Cochrane
    Replies:
    2
    Views:
    637
    Keith Cochrane
    Aug 6, 2006
Loading...

Share This Page