Unix Timestamp와 .NET DateTime 간의 상호변환

Silverlight/Lecture 2008.05.09 14:13

Unix Timestamp를 .NET DateTime으로 변환할 때

static DateTime ConvertFromUnixTimestamp(int timestamp)
{
    DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
    return origin.AddSeconds((double)timestamp);
}

.NET DateTime을 Unix Timestamp로 변환할 때

static int ConvertToUnixTimestamp(DateTime date)
{
    DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
    TimeSpan diff = date - origin;
    return (int)Math.Floor(diff.TotalSeconds);
}

참고 : http://codeclimber.net.nz/archive/2007/07/10/Convert-a-Unix-timestamp-to-a-.NET-DateTime.aspx


설정

트랙백

댓글