473,320 Members | 1,732 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Conver Time to Seconds

How can I convert today's time (9:30:00) to seconds (34200) without
using calculations like (9*3600)+(30*60)+0? Is there an easy function?
Thank you

Jan 11 '07 #1
8 2550
Lee
vu******@gmail.com said:
>
How can I convert today's time (9:30:00) to seconds (34200) without
using calculations like (9*3600)+(30*60)+0? Is there an easy function?
Thank you
var now=new Date();
var midnight=new Date(now.getFullYear(),now.getMonth(),now.getMinut es());
var nowSec=Math.floor((now.getTime()-midnight.getTime())/1000);
alert(nowSec);
--

Jan 11 '07 #2
vu******@gmail.com wrote:
How can I convert today's time (9:30:00) to seconds (34200) without
using calculations like (9*3600)+(30*60)+0? Is there an easy function?
Well I guess that:

function foo(hours,minutes,seconds) {
return hours*3600+minutes*60+seconds;
}

....is really simple, if you don't like it then you can probably use the
Date Object, for instance something like:

function foo(hours, minutes, seconds) {
return Date.UTC(1970,0,1,hours,minutes,seconds)/1000;
}

You don't state your exact goal so it is hard to advise, however... I
wonder whether the day matters in your calculation (if so, you could
need to take time change into account).
Regards,
Elegie.
Jan 11 '07 #3
Thank you for advice. I receive data as 093000. So I need to calculate
the seconds from the beginning of this day out of that number. Ideally,
the number above should be 34200. So, I devide the number by pieces
like 09:30:00 and then every element is multiplied by seconds.
This seems to be a insufficient way. Do you think you can share any
ideas of how you see this could be done?
Thank you all.

Jan 11 '07 #4
Lee
vu******@gmail.com said:
>
Thank you for advice. I receive data as 093000. So I need to calculate
the seconds from the beginning of this day out of that number. Ideally,
the number above should be 34200. So, I devide the number by pieces
like 09:30:00 and then every element is multiplied by seconds.
This seems to be a insufficient way. Do you think you can share any
ideas of how you see this could be done?
You should look up how to quote the messages you're responding to.
I know that Google Groups makes this awkward.

Whatever method you use is going to involve the sort of multiplication
you seem to want to avoid. Having it done behind the scenes in a
method of Date() doesn't really make it more efficient.
--

Jan 11 '07 #5
In comp.lang.javascript message <11**********************@77g2000hsv.goo
glegroups.com>, Thu, 11 Jan 2007 12:06:51, vu******@gmail.com posted:
>How can I convert today's time (9:30:00) to seconds (34200) without
using calculations like (9*3600)+(30*60)+0? Is there an easy function?
Thank you
If that task is too difficult for you, have you considered janitoring or
politics as a career?

If that time is in a string :

S = "9:30:00"
D = "1/1/1 "
s = ( new Date(D+S) - new Date(D) )/1000

If the current date is used for D, that will even give a correct result
on the Second Sunday of March and the first Sunday of November (I assume
you are American), when your calculation will not give the right result.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6.
Web <URL:http://www.merlyn.demon.co.uk/- w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/- see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
Jan 11 '07 #6
In comp.lang.javascript message <eo********@drn.newsguy.com>, Thu, 11
Jan 2007 13:08:04, Lee <RE**************@cox.netposted:
>vu******@gmail.com said:
>>
How can I convert today's time (9:30:00) to seconds (34200) without
using calculations like (9*3600)+(30*60)+0? Is there an easy function?
Thank you
var now=new Date();
var midnight=new Date(now.getFullYear(),now.getMonth(),now.getMinut es());
var nowSec=Math.floor((now.getTime()-midnight.getTime())/1000);
alert(nowSec);
If you take a copy of my js-quick.htm for your own computer, you can
test code before you post it.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Jan 12 '07 #7
In comp.lang.javascript message <11*********************@o58g2000hsb.goo
glegroups.com>, Thu, 11 Jan 2007 13:54:27, vu******@gmail.com posted:
>Thank you for advice. I receive data as 093000.
If it actually has a leading zero, then it must be a String.
So I need to calculate
the seconds from the beginning of this day out of that number.
Of which day?
Ideally,
the number above should be 34200. So, I devide the number by pieces
like 09:30:00 and then every element is multiplied by seconds.
This seems to be a insufficient way. Do you think you can share any
ideas of how you see this could be done?
You should consider the question of whether the input data may be in
error. Should 093060 be treated as 093100 or should it give NaN or
undefined or other error indication?

Two methods :
S = "093000"
R = /(\d\d)(\d\d)(\d\d)/

D = "1/1/1 " // or today
s = ( new Date(D+S.replace(R, "$1:$2:$3")) - new Date(D) )/1000

if (s = S.match(R)) s = s[1]*3600 + s[2]*60 + +s[3]
It's a good idea to read the newsgroup and its FAQ. See below.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Jan 12 '07 #8
Lee
Dr J R Stockton said:
>
In comp.lang.javascript message <eo********@drn.newsguy.com>, Thu, 11
Jan 2007 13:08:04, Lee <RE**************@cox.netposted:
>>vu******@gmail.com said:
>>>
How can I convert today's time (9:30:00) to seconds (34200) without
using calculations like (9*3600)+(30*60)+0? Is there an easy function?
Thank you
var now=new Date();
var midnight=new Date(now.getFullYear(),now.getMonth(),now.getMinut es());
var nowSec=Math.floor((now.getTime()-midnight.getTime())/1000);
alert(nowSec);

If you take a copy of my js-quick.htm for your own computer, you can
test code before you post it.
I did test it, at a time of day that allowed it to appear to work
(my local clock having gained a few minutes, apparently).
Knowing that it contains a glaring error, the correction is left
as an exercise.
--

Jan 13 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: kpp9c | last post by:
I am kind of in a bit of a jam (okay a big jam) and i was hoping that someone here could give me a quick hand. I had a few pages of time calculations to do. So, i just started in on them typing...
77
by: Charles Law | last post by:
Hi guys I have a time critical process, running on a worker thread. By "time critical", I mean that certain parts of the process must be completed in a specific time frame. The time when the...
5
by: Tom | last post by:
A field in a data set I want to import into Access is in Unix time (seconds from a certain time on a certain date). Does anyone know the precise date and the precise time on that date that Unix is...
5
by: Sinan Nalkaya | last post by:
hello, i need a function like that, wait 5 seconds: (during wait) do the function but function waits for keyboard input so if you dont enter any it waits forever. i tried time.sleep() but when...
5
by: Summu82 | last post by:
HI I have to convert a time in the format i have year month day hour min and seconds I need to convert this into time in seconds since 1 jan 1970 i.e the
7
by: nono909 | last post by:
I wrote the following time class for the following assignment.i need help in completing this program pleasee. Write a class to hold time. Time is the hour, minute and seconds. Write a constructor...
3
by: Paul McGuire | last post by:
I am doing some simple timing of some elements of Python scripts, and the simplest is to just call time.time() before and after key elements of the script: t1 = time.time() # do lengthy...
8
by: Theo v. Werkhoven | last post by:
hi, In this code I read out an instrument during a user determined period, and save the relative time of the sample (since the start of the test) and the readback value in a csv file. #v+...
0
by: Hendrik van Rooyen | last post by:
Lawrence D'Oliveiro wrote: The above is an endless loop, doing something every five seconds, and something else in the in between times. I don't think you are dense - its a good point,...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.