Unix date format in Visual Basic.net

M

Marko Maehner

Hello!

I have developped a PocketPC application with CF and VB.net. Now there
are some new features to implement and in one case I have to send a
request to a WebService with several arguments and one of them is the
current date in an unix format (seconds since 01.01.1970).

Is there a possibility or an existing function to convert the current
date (now()) to this date/time format? Has someone an existing code
snipplet for this?


Thanks in advance,
Marko
 
J

Jay B. Harlow [MVP - Outlook]

Marko,
The easiest way may be to use the TotalSeconds property of a Timespan.

Something like:
Dim d As DateTime = DateTime.Now
Dim ts As TimeSpan = d.Subtract(#1/1/1970#)
Dim secs As Integer = CInt(ts.TotalSeconds)

To convert a "unix format" back to DateTime you can simply add the seconds
to 1/1/70

d = #1/1/1970#.AddSeconds(secs)

Note #1/1/1970# is a date literal, which is always month, day, year
independent of the current local...

I was thinking .NET had a 'function' to do this, however I am not seeing it
right now...

Hope this helps
Jay
 
G

Guest

..NET to Unix

Dim ts as TimeSpan = (DateTime.UtcNow - new DateTime(1970,1,1,0,0,0));
Dim unixTime as Double = ts.TotalSeconds;


Unix To .NET
(new DateTime(1970,1,1,0,0,0)).AddSeconds(unixtimestamp)

hth
Richard
 

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