Comparing two 12hr time fields

Discussion in 'Javascript' started by M.i.r.a.g.e., Aug 30, 2004.

  1. M.i.r.a.g.e.

    M.i.r.a.g.e. Guest

    I am sorry if this has been asked before, but I looked all over for
    exactly what I need, before asking you guys. I know that it has
    something to do with the Date object, but I'm not sure how to
    manipulate it.

    I have 2 input fields, startTime, and finishTime.
    The times need to be entered like 7:30am or 11:36pm.

    All I want is to subtract the finishTime from the startTime to see how
    long something took. I almost had it once, except that if I entered
    the following:
    startTime="7:00am" and finishTime="1:00am" (next day), it displayed "6
    hours", instead of "18 hours".

    I know that this should be real easy, but I just can't find this
    solution, and I've been to alot of sites.

    Could someone please post a snippet that can do this, or at least a
    link to a page that has one. Thanks.
    M.i.r.a.g.e., Aug 30, 2004
    #1
    1. Advertising

  2. M.i.r.a.g.e.

    Evertjan. Guest

    M.i.r.a.g.e. wrote on 30 aug 2004 in comp.lang.javascript:

    > I am sorry if this has been asked before, but I looked all over for
    > exactly what I need, before asking you guys. I know that it has
    > something to do with the Date object, but I'm not sure how to
    > manipulate it.
    >
    > I have 2 input fields, startTime, and finishTime.
    > The times need to be entered like 7:30am or 11:36pm.
    >
    > All I want is to subtract the finishTime from the startTime to see how
    > long something took. I almost had it once, except that if I entered
    > the following:
    > startTime="7:00am" and finishTime="1:00am" (next day), it displayed "6
    > hours", instead of "18 hours".
    >
    > I know that this should be real easy, but I just can't find this
    > solution, and I've been to alot of sites.
    >
    > Could someone please post a snippet that can do this, or at least a
    > link to a page that has one. Thanks.


    If you really must use this antiquated form of time notation,
    show us the code of your present attempt.
    Rather than just coding for you,We better try to improve your own ideas,
    if reasonably good.

    btw,
    did you look at the faq yet?
    is the finish always after the start and no more than 24 hours apart?
    Do we define a day as 24 hours without summertime switch hour?

    -
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress,
    but let us keep the discussions in the newsgroup)
    Evertjan., Aug 30, 2004
    #2
    1. Advertising

  3. JRS: In article <>,
    dated Mon, 30 Aug 2004 09:34:13, seen in news:comp.lang.javascript,
    M.i.r.a.g.e. <> posted :
    >I am sorry if this has been asked before, but I looked all over for
    >exactly what I need, before asking you guys. I know that it has
    >something to do with the Date object, but I'm not sure how to
    >manipulate it.


    Can you explain how it was that the regularly-posted newsgroup FAQ was
    insufficiently helpful?


    >I have 2 input fields, startTime, and finishTime.
    >The times need to be entered like 7:30am or 11:36pm.


    Ahh - evidently a Stranger. Those are FFF times; deprecated.


    >All I want is to subtract the finishTime from the startTime to see how
    >long something took.


    Most people would do the reverse.


    >I know that this should be real easy,


    Correct. It can be done in various ways.

    Z = "1970/01/01 " // Should not be a Summer Time Change Date
    ST = "7:00am"
    FT = "11:36pm"

    D = new Date(Z+FT) - new Date(Z+ST)
    Ans = new Date(D).toUTCString().replace(/^.* ([^ ]{8}) .*$/, "$1")

    -> Ans = "16:36:00" // Assumes that ST <= FT and interval < 24h

    I assume that toUTCString uses the 24-h clock in all locations.

    If you give only the times, one must ignore source Summer Time changes -
    indeed also other time shifts.

    To get hh mm ss separately, use D.getUTCHours() etc.

    Test in a location where civil time is never GMT or UTC.

    --
    © 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 30, 2004
    #3
  4. JRS: In article <>, dated Mon, 30
    Aug 2004 22:44:15, seen in news:comp.lang.javascript, Dr John Stockton
    <> posted :
    >
    > D = new Date(Z+FT) - new Date(Z+ST)
    > Ans = new Date(D).toUTCString().replace(/^.* ([^ ]{8}) .*$/, "$1")


    or

    D = new Date(Z+FT) - new Date(Z+ST)
    Ans = new Date(D).toUTCString().match(/([\d:]{8})/g)

    --
    © 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 31, 2004
    #4
  5. M.i.r.a.g.e.

    M.i.r.a.g.e. Guest

    Thanks guys!
    M.i.r.a.g.e., Sep 2, 2004
    #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. flamesrock
    Replies:
    8
    Views:
    451
    Hendrik van Rooyen
    Nov 24, 2006
  2. Tamer Ibrahim
    Replies:
    1
    Views:
    577
    JIGNESH
    Jan 17, 2008
  3. AMT2K5
    Replies:
    1
    Views:
    193
    Eric Schwartz
    Nov 8, 2005
  4. Replies:
    3
    Views:
    127
    John W. Krahn
    Nov 28, 2007
  5. The guy upstairs

    comparing two email fields

    The guy upstairs, Nov 19, 2006, in forum: Javascript
    Replies:
    2
    Views:
    105
    mick white
    Nov 19, 2006
Loading...

Share This Page