Milliseconds From 1970!

S

Steve

I have a value that represents the time in milliseconds passed since
1970 00:00:00 GMT that I need to convert into a readable date e.g.

1092364211773 = 13/08/2004 03:30:00 GMT

Is this easily done or do I need to write my own function for this???
If that's the case, does anyone have some code to do this???

Thanks in advance

Steve
 
J

Jay B. Harlow [MVP - Outlook]

Steve,
Have you tried the DateTime.AddMilliseconds method?

Something like:

Dim base As New DateTime(1970, 1, 1)
Dim readableDate As DateTime = base.AddMilliseconds(1092364211773)
Debug.WriteLine(readableDate, "Readable date")

Hope this helps
Jay
 
J

Jon Skeet [C# MVP]

Steve said:
I have a value that represents the time in milliseconds passed since
1970 00:00:00 GMT that I need to convert into a readable date e.g.

1092364211773 = 13/08/2004 03:30:00 GMT

Is this easily done or do I need to write my own function for this???
If that's the case, does anyone have some code to do this???

It's easy to create a DateTime for Jan 1st 1970, and a TimeSpan for
however many milliseconds you've got - then just add one to the other.
 
S

Steve

This is perfect, thank you so much...

Steve
-----Original Message-----
Steve,
Have you tried the DateTime.AddMilliseconds method?

Something like:

Dim base As New DateTime(1970, 1, 1)
Dim readableDate As DateTime = base.AddMilliseconds(1092364211773)
Debug.WriteLine(readableDate, "Readable date")

Hope this helps
Jay




.
 
J

Jay B. Harlow [MVP - Outlook]

Steve,
If you go the TimeSpan route as Jon suggests. You can use
TimeSpan.FromMilliseconds to get a TimeSpan with milliseconds in it.

Dim base As New DateTime(1970, 1, 1)
Dim ts As TimeSpan = TimeSpan.FromMilliseconds(1092364211773)
Dim readableDate As DateTime = base.Add(ts)

Debug.WriteLine(readableDate, "Readable date")

I'm mentioning this as I "always" forget about the TimeSpan.From* methods...
*shrug*

Hope this helps
Jay
 
C

Cowboy \(Gregory A. Beamer\) [MVP]

You can set up a DateTime Structure for the date in 1970 and then use a
TimeSpan object with the requisite number of milliseconds and add to the
first DateTime to get another DateTime.

Dim dt as DateTime = Convert.ToDateTime("1/1/1970")

'Check this next line to make sure it is in the millisecond value
Dim ts As New TimeSpan(0,0,0,0,1092364211773)

Dim dt as DateTime = dt + ts

Something like that. Check the documentation if this blows chunks, but the
theory is sound.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************
Think Outside the Box!
************************************************
 
C

Cor Ligthert

George,

In my opinion it is better to use as Jay showed
Dim base As New DateTime(1970, 1, 1)

That is independent from the culture settings.
(Assuming the time Steve is from the US however when others read this).

Cor
 

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