300x250 AD TOP

Search This Blog

Paling Dilihat

Powered by Blogger.

Tuesday, April 10, 2012

Unix to DateTime to Unix

If you need to convert from .NET DateTime to Unix datetime and back, here's how:


/// <summary>
/// Converts a unix datetime to C# DateTime
/// </summary>
/// <param name="unixTimeStamp"></param>
/// <returns></returns>
public static DateTime UnixTimeStampToDateTime(double unixTimeStamp)
{
    // Unix timestamp is seconds past epoch
    System.DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
    dtDateTime = dtDateTime.AddSeconds(unixTimeStamp).ToUniversalTime();
    return dtDateTime;
}


/// <summary>
/// Converts C# DateTime to unix datetime
/// </summary>
/// <param name="dateTime"></param>
/// <returns></returns>
public static double DateTimeToUnixTimestamp(DateTime dateTime)
{
    return (dateTime.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).ToUniversalTime()).TotalSeconds;
}


From here.

Tags:

0 comments:

Post a Comment