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
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();
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.
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.
{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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With