When integrating various systems you sometimes need to convert DateTime values to an Integer, the code samples below will be helpful
Public Function UnixToDateTime(ByVal strUnixTime As String) As Date UnixToDateTime = DateAdd(DateInterval.Second, Val(strUnixTime), #1/1/1970#) End Function Public Function DateTimeToUnix(ByVal dteDate As Date) As String DateTimeToUnix = System.Convert.ToInt64((dteDate - New DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds) End Function
public static long DateTimeToUnix(this DateTime target) { var date = new DateTime(1970, 1, 1, 0, 0, 0, target.Kind); var unixTimestamp = System.Convert.ToInt64((target - date).TotalSeconds); return unixTimestamp; } public static DateTime UnixToDateTime(this DateTime target, long timestamp) { var dateTime = new DateTime(1970, 1, 1, 0, 0, 0, target.Kind); return dateTime.AddSeconds(timestamp); }
Last Updated on October 26, 2015