Comparing two 12hr time fields

M

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

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.
 
E

Evertjan.

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)
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Mon, 30 Aug 2004 09:34:13, seen in
M.i.r.a.g.e. said:
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.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Mon, 30
Aug 2004 22:44:15, seen in Dr John Stockton
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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top