Convert timestamp to datetime format?

Montie

Member
Joined
Feb 16, 2004
Messages
15
How can I convert timestamp data that I got from an XML into a "normal" datetime format, that I would prefer (AND vice versa)? Of course, using Visual Basic .NET...

Thanks!
 
Timestamp is number of second since January 1st, 1970. For example - 134343235223444.

I mentioned that I got it from XML, but it really doesnt matter where I get it from, it could be from a text file. Can I use anything else then XMLConvert? How do I convert VB DateTime type to timestamp?
 
Try this:
Code:
        Dim Dnought As DateTime = #1/1/1970#
        January 1st, 1970 into this datetime.
The ## means that its a datetime constant.
        Dim Dfinal As DateTime = Dnought.AddSeconds(1343432352)
Add the number of seconds to dnought to get the final date.
        MessageBox.Show(Dfinal.ToString())

The number that I have there gets you something in 2012. The number you have nets you something a couple hundred millenia in the future :).
 
Ali G would say "Aaaaiight"! ;) I say, it looks like a nice suggestion. Thanks! :)
What do you think about converting timestamp to datetime? Should I hustle with the seconds, or is there a smoother way out?

p.s. Yes, I know, I really didnt check the number, I just put it on the road ;)
 
It will work...

Dim d1970 As DateTime = #1/1/1970#
Dim dNow As New DateTime.Now
calculate timestamp
mTS = DateDiff(DateInterval.Second, dNow, d1970)
 
You are probably better of using the native .Net functions rather than the legacy VB6 DateDiff

it should be something like
Code:
Dim d1970 As DateTime = #1/1/1970#
Dim dNow As New DateTime.Now
Dim diff as TimeSpan
diff = dNow.Subtract(d1970)
mTS = ts.TotalSeconds
 
Back
Top