Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Comparing two 12hr time fields

Reply
Thread Tools

Comparing two 12hr time fields

 
 
M.i.r.a.g.e.
Guest
Posts: n/a
 
      08-30-2004
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.
 
Reply With Quote
 
 
 
 
Evertjan.
Guest
Posts: n/a
 
      08-30-2004
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)

 
Reply With Quote
 
 
 
 
Dr John Stockton
Guest
Posts: n/a
 
      08-30-2004
JRS: In article <(E-Mail Removed) >,
dated Mon, 30 Aug 2004 09:34:13, seen in news:comp.lang.javascript,
M.i.r.a.g.e. <(E-Mail Removed)> 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.
 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      08-31-2004
JRS: In article <(E-Mail Removed)>, dated Mon, 30
Aug 2004 22:44:15, seen in news:comp.lang.javascript, Dr John Stockton
<(E-Mail Removed)> 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.
 
Reply With Quote
 
M.i.r.a.g.e.
Guest
Posts: n/a
 
      09-02-2004
Thanks guys!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Parsing two files and comparing the first fields.. clearguy02@yahoo.com Perl Misc 3 11-28-2007 11:54 PM
comparing two email fields The guy upstairs Javascript 2 11-19-2006 04:10 PM
Comparing fields bvlmv@hotmail.com ASP General 3 10-26-2006 10:18 AM
Newbie - Reading a file with delimited fields, storing first two fields in a hash AMT2K5 Perl Misc 1 11-08-2005 01:06 AM
Newbie Question: JS for Comparing Email Fields...Use with CGI Script?? bateman5 Javascript 10 02-17-2004 12:51 PM



Advertisments