Solved

# Convert date to unix date

Posted on 2006-05-14
488 Views
Last Modified: 2012-08-14
how can i convert todays date/time into a unix date stamp?
http://en.wikipedia.org/wiki/Unix_time
0
Question by:bctek
9 Comments

LVL 44

Expert Comment

Hi bctek,

you could look at this message
source: http://groups.google.com/group/microsoft.public.dotnet.languages.vb/msg/1363b11c9a6d0292?hl=en&
--------
'.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)
--------

hope this helps a bit
bruintje
0

Author Comment

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

LVL 44

Expert Comment

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 Comment

hi bruintje.  i took a screenshot of the error:

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

LVL 17

Accepted Solution

Have you tried the DateDiff function?

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

LVL 44

Expert Comment

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

LVL 44

Expert Comment

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

LVL 44

Expert Comment

sorry had to post
Dim ts as TimeSpan = (Date.UtcNow - new Date(1970,1,1,0,0,0))
0

Author Comment

the datediff worked perfectly.  sorry bruintje, that code kept generating the same error.  i use vs.net
0

## Write Comment

Please enter a first name

Please enter a last name

We will never share this with anyone.

## Featured Post

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

#### 779 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

#### Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!