Comparing two 12hr time fields | | |
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. | | | | re: Comparing two 12hr time fields
M.i.r.a.g.e. wrote on 30 aug 2004 in comp.lang.javascript:
[color=blue]
> 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.[/color]
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) | | | | re: Comparing two 12hr time fields
JRS: In article <eb0534a6.0408300834.345c0e72@posting.google.com >,
dated Mon, 30 Aug 2004 09:34:13, seen in news:comp.lang.javascript,
M.i.r.a.g.e. <Win_XP_User@hotmail.com> posted :[color=blue]
>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.[/color]
Can you explain how it was that the regularly-posted newsgroup FAQ was
insufficiently helpful?
[color=blue]
>I have 2 input fields, startTime, and finishTime.
>The times need to be entered like 7:30am or 11:36pm.[/color]
Ahh - evidently a Stranger. Those are FFF times; deprecated.
[color=blue]
>All I want is to subtract the finishTime from the startTime to see how
>long something took.[/color]
Most people would do the reverse.
[color=blue]
>I know that this should be real easy,[/color]
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. | | | | re: Comparing two 12hr time fields
JRS: In article <Bwd8A9Av+5MBFwNr@merlyn.demon.co.uk>, dated Mon, 30
Aug 2004 22:44:15, seen in news:comp.lang.javascript, Dr John Stockton
<spam@merlyn.demon.co.uk> posted :[color=blue]
>
> D = new Date(Z+FT) - new Date(Z+ST)
> Ans = new Date(D).toUTCString().replace(/^.* ([^ ]{8}) .*$/, "$1")[/color]
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. | | | | re: Comparing two 12hr time fields
Thanks guys! |  | Similar JavaScript / Ajax / DHTML bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,471 network members.
|