By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,442 Members | 1,368 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,442 IT Pros & Developers. It's quick & easy.

Comparing two 12hr time fields

P: n/a
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.
Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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)

Jul 23 '05 #2

P: n/a
JRS: In article <eb**************************@posting.google.com >,
dated Mon, 30 Aug 2004 09:34:13, seen in news:comp.lang.javascript,
M.i.r.a.g.e. <Wi*********@hotmail.com> 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.
Jul 23 '05 #3

P: n/a
JRS: In article <Bw**************@merlyn.demon.co.uk>, dated Mon, 30
Aug 2004 22:44:15, seen in news:comp.lang.javascript, Dr John Stockton
<sp**@merlyn.demon.co.uk> 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.
Jul 23 '05 #4

P: n/a
Thanks guys!
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.