C++ DateTime to and from C#

Feb 8 2008 3:02 PM

I had a need to convert c++ time_t to C# DateTime and have also had the same need to convert to C# DateTime to c++ time_t.

I hope some of you find these conversions useful

[code]

public static long ToUnixtime(DateTime date)

{

DateTime unixStartTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);

TimeSpan timeSpan = date - unixStartTime;

return Convert.ToInt64(timeSpan.TotalSeconds);

}

public static DateTime ToCSharpTime(long unixTime)

{

DateTime unixStartTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);

return unixStartTime.AddSeconds(Convert.ToDouble(unixTime));

}

[/CODE]

 


Answers (1)