Total number of seconds

  • Thread starter Thread starter Chris Leffer
  • Start date Start date
C

Chris Leffer

Hi.

My application reads a table that stores data as time, eg: 02:17:31

I need to read this value and convert it to an integer, representing the
total number of seconds. At this moment I am using code like this:
CInt(CDate(drTime(2)).Hour / 3600) + CInt(CDate(drTime(2)).Minute * 60)
+ CInt(CDate(drTime(2)).Second)

Is it the only way to get the total number of seconds? I tried the
TimeSpan object and the TotalSeconds property, but the values I received
were not correct.

Regards,
Chris Leffer
 
Chris Leffer said:
Hi.

My application reads a table that stores data as time, eg: 02:17:31

I need to read this value and convert it to an integer, representing the
total number of seconds. At this moment I am using code like this:
CInt(CDate(drTime(2)).Hour / 3600) + CInt(CDate(drTime(2)).Minute * 60)
+ CInt(CDate(drTime(2)).Second)

Is it the only way to get the total number of seconds? I tried the
TimeSpan object and the TotalSeconds property, but the values I received
were not correct.

dim d as DateTime ...
dim seconds as integer = cint(d.TimeOfDay.TotalSeconds)

David
 
Chirs,

Shouldn't you be multiplying the hour times 3600, not dividing by 3600?

Here is some code that uses Timespan and gets the same result as manually
calculating from the components of datetime:

Dim myDT As New DateTime
myDT = Now
MsgBox(myDT)

Dim myTS As New TimeSpan(myDT.Hour, myDT.Minute, myDT.Second)
MsgBox(myTS.TotalSeconds)

MsgBox(myDT.Hour * 3600 + myDT.Minute * 60 + myDT.Second)

Kerry Moorman
 

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

Back
Top