• Status: Solved
• Priority: Medium
• Security: Public
• Views: 520

# Convert date to unix date

how can i convert todays date/time into a unix date stamp?
http://en.wikipedia.org/wiki/Unix_time
0
bctek
• 5
• 3
1 Solution

Commented:
Hi bctek,

you could look at this message
--------
'.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
--------

hope this helps a bit
bruintje
0

Author Commented:
cant get the code to work.  :(

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

this line generates an error.
0

Commented:
what was the error? i tried it here

'.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
Dim winTime As Date = New DateTime(1970, 1, 1, 0, 0, 0).AddSeconds(unixtimestamp)
0

Author Commented:
hi bruintje.  i took a screenshot of the error:

www.songwave.com/ttemp/vberror.jpg
0

Commented:
Have you tried the DateDiff function?

Dim unixTime As Long = DateDiff(DateInterval.Second, New DateTime(1970, 1, 1, 0, 0, 0), DateTime.Now)
0

Commented:
strange i use vb express could that make a difference?
0

Commented:
i changed it to
Dim ts as TimeSpan = (DateTime.UtcNow - new Date(1970,1,1,0,0,0))

with the same outcome but more importantly makes it compare 2 dates
0

Commented:
Dim ts as TimeSpan = (Date.UtcNow - new Date(1970,1,1,0,0,0))
0

Author Commented:
the datediff worked perfectly.  sorry bruintje, that code kept generating the same error.  i use vs.net
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.