Parse Seconds to Time AM/PM

J

james

Banging my head here. I have an older Database that I have imported to Access and the original database system (Dataflex)
stored the Time of Day in Seconds. I can use :TimeSpan.FromSeconds(74400),, to get a time of: 20:40 (24hr time).
I need to get 8:40 PM . But, looking thru all the different methods of TimeSpan I cannot find how this is done short of writing
a routine that looks at the Hours and Seconds to determine AM/PM . I also looked thru the DateTime function and although it
will accept Seconds, it
shows : 01/01/0001 02:04 (if I remember correctly) which is the date and time from the beginning of time
( VB.NET time) I have looked at the Parse method and cannot find any examples on how this is done.
And Google doesn't seem to find anything either.
I would think there would be a simple parsing method that would allow you to display the time as 12Hr. or 24Hr time. But, I
cannot find anything to show that.
Any info would be greatly appreciated.
james

(I'm betting it's going to be simple)
 
C

Chris Dunaway

james wrote:
looked thru the DateTime function and although it
will accept Seconds, it
shows : 01/01/0001 02:04 (if I remember correctly) which is the date and time from the beginning of time
I would think there would be a simple parsing method that would allow you to display the time as 12Hr. or 24Hr time. But, I

A DateTime variable will always have both a date and time component.
To display just the time, use the ToString method of the DateTime
variable:

Dim dtCurrTime As New DateTime(0,0,0,0,0,74400)

MsgBox("Current Time is: " & dtCurrTime.ToString("hh:mm tt"))

'Result: Current Time is: 08:40 PM

MsgBox("Current Time is: " & dtCurrTime.ToString("HH:mm"))

'Result: Current Time is: 20:40

HTH

Chris
 
J

james

Chris Dunaway said:
james wrote:
looked thru the DateTime function and although it

A DateTime variable will always have both a date and time component.
To display just the time, use the ToString method of the DateTime
variable:

Dim dtCurrTime As New DateTime(0,0,0,0,0,74400)

MsgBox("Current Time is: " & dtCurrTime.ToString("hh:mm tt"))

'Result: Current Time is: 08:40 PM

MsgBox("Current Time is: " & dtCurrTime.ToString("HH:mm"))

'Result: Current Time is: 20:40

HTH

Chris

Chris, thanks for the info, using the example code, I get : Specified argument was out of the range of valid values. Which
doesn't make sense to me as there are 86,400 seconds in a 24 hr period. So, as your example shows, it should show the time
correctly as 74400 is not out of range. Maybe, I copied something wrong.
I will keep playing with it.
james
 
J

james

Chris Dunaway said:
A DateTime variable will always have both a date and time component.
To display just the time, use the ToString method of the DateTime
variable:

Dim dtCurrTime As New DateTime(0,0,0,0,0,74400)

MsgBox("Current Time is: " & dtCurrTime.ToString("hh:mm tt"))

'Result: Current Time is: 08:40 PM

MsgBox("Current Time is: " & dtCurrTime.ToString("HH:mm"))

'Result: Current Time is: 20:40

HTH

Chris

Chris, just an update, I even copied and pasted this code:

Dim dtCurrTime As New DateTime(0,0,0,0,0,74400)

MsgBox("Current Time is: " & dtCurrTime.ToString("hh:mm tt"))


And got this error:

"An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll

Additional information: Specified argument was out of the range of valid values."


If I put it in a Try-Catch block it shows this error:


System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.

Parameter name: Year, Month, and Day parameters describe an unrepresentable DateTime.

at System.DateTime.DateToTicks(Int32 year, Int32 month, Int32 day)

at System.DateTime..ctor(Int32 year, Int32 month, Int32 day, Int32 hour, Int32 minute, Int32 second)

at Seconds_to_Time.Form1.Button1_Click(Object sender, EventArgs e) in C:\Documents and Settings\James\My Documents\Visual Studio
Projects\VS2003Beta\Seconds to Time\Seconds to Time\Form1.vb:line 112


So, I am a bit lost as what to do. Just thought you might want to see the results I have got.
Thanks for your help.
james
 
C

Chris Dunaway

I'm sorry, that is what I call thin air code. Change the declaration
to:

Dim dtCurrTime As new DateTime(1,1,1,0,0,74400)

That sets the date portion to a valid date of jan 1, 0001. When you do
the format, it only gets the time. Or you can use the TimeValue
property of the DateTime.
 
J

james

Chris Dunaway said:
I'm sorry, that is what I call thin air code. Change the declaration
to:

Dim dtCurrTime As new DateTime(1,1,1,0,0,74400)

That sets the date portion to a valid date of jan 1, 0001. When you do
the format, it only gets the time. Or you can use the TimeValue
property of the DateTime.

There must be something in my system setup that is causing this, using the same declaration that you included here, I still get
the same error messages. I do appreciate your help. I will eventually track this
problem down and once I do, I will post a follow-up.
james
 

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

Top