DateTime to UnixTime Stamp in .net

Is there any function in vb dot net for converting date and time to unix time stamp If I google, I get just the opposite, but not vb.net for unix timestamp

Any help is appreciated

+5
source share
4 answers

{Modify} removed old link link

Seconds from January 1, 1970 = unix time

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

Dim uTime As Integer
uTime = (DateTime.UtcNow - New DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds
+17
source

- - , .

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
+5

http://www.codeproject.com/KB/cs/timestamp.aspx, unix → . net, :

DateTime dt = "the date";
DateTime start= new System.DateTime(1970, 1, 1, 0, 0, 0, 0);

TimeSpan ts = (dt - start);

ts.TotalSeconds //unix timestamp

- .

, , , , :)

+1

javascript, , unix , , . .Net, 10 000 /. , :

Public Module UnixTime
    Const UnixEraStartTicks As Long = 621355968000000000
    <Extension> Public Function UnixTimestamp(value As Date) As Long
        Dim UnixEraTicks = value.Ticks - UnixEraStartTicks
        Return UnixEraTicks \ 10000
    End Function
    Public Function DateFromUnix(timestamp As Long) As Date
        Return New Date(UnixEraStartTicks + timestamp * 10000)
    End Function
End Module
0

All Articles