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);
}
Originally Posted on October 23, 2013
Last Updated on October 26, 2015
Last Updated on October 26, 2015
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.