Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTime to UnixTime Stamp in .net

Is there any function in vb dot net to convert datetime to unix time stamp If I google I get only the vice versa but not vb.net to unix time stamp

Any help is appreciated

like image 296
Ramji Avatar asked Dec 21 '09 20:12

Ramji


People also ask

How to convert DateTime to Unix time in c#?

The trick is to convert the DateTime into a DateTimeOffset, and convert it from there to the UNIX timestamp number. string unixTimeMilliSeconds = dto. ToUnixTimeMilliseconds(). ToString();

How do I convert DateTime to TimeSpan?

To convert a DateTime to a TimeSpan you should choose a base date/time - e.g. midnight of January 1st, 2000, and subtract it from your DateTime value (and add it when you want to convert back to DateTime ). If you simply want to convert a DateTime to a number you can use the Ticks property. Show activity on this post.

What is epoch format?

In a computing context, an epoch is the date and time relative to which a computer's clock and timestamp values are determined. The epoch traditionally corresponds to 0 hours, 0 minutes, and 0 seconds (00:00:00) Coordinated Universal Time (UTC) on a specific date, which varies from system to system.


2 Answers

{Edit} removed old reference link

Seconds since Jan 1, 1970 = unix time

To get this in VB.NET see below example (in example using DateTime.UtcNow, but you can plug in any DateTime you want there)

Dim uTime As Double
uTime = (DateTime.UtcNow - New DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds
like image 59
curtisk Avatar answered Sep 24 '22 04:09

curtisk


I wrote these at some point - all similar to the above just with a little more detail on the old summer time adjustment thing for me here in the UK.

Public Function UnixToTime(ByVal strUnixTime As String) As Date
    UnixToTime = DateAdd(DateInterval.Second, Val(strUnixTime), #1/1/1970#)
    If UnixToTime.IsDaylightSavingTime = True Then
        UnixToTime = DateAdd(DateInterval.Hour, 1, UnixToTime)
    End If
End Function

Public Function TimeToUnix(ByVal dteDate As Date) As String
    If dteDate.IsDaylightSavingTime = True Then
        dteDate = DateAdd(DateInterval.Hour, -1, dteDate)
    End If
    TimeToUnix = DateDiff(DateInterval.Second, #1/1/1970#, dteDate)
End Function
like image 44
Ian Avatar answered Sep 23 '22 04:09

Ian