我需要從32位DOS日期轉換爲.NET System.DateTime並再次返回。我使用下面的兩個例程,但是當我將它們來回轉換時,它們會出現幾秒鐘。任何人都能看到爲什麼.Net DateTime到DOS日期32位轉換
public static DateTime ToDateTime(this int dosDateTime)
{
var date = (dosDateTime & 0xFFFF0000) >> 16;
var time = (dosDateTime & 0x0000FFFF);
var year = (date >> 9) + 1980;
var month = (date & 0x01e0) >> 5;
var day = date & 0x1F;
var hour = time >> 11;
var minute = (time & 0x07e0) >> 5;
var second = (time & 0x1F) * 2;
return new DateTime((int)year, (int)month, (int)day, (int)hour, (int)minute, (int)second);
}
public static int ToDOSDate(this DateTime dateTime)
{
var years = dateTime.Year - 1980;
var months = dateTime.Month;
var days = dateTime.Day;
var hours = dateTime.Hour;
var minutes = dateTime.Minute;
var seconds = dateTime.Second;
var date = (years << 9) | (months << 5) | days;
var time = (hours << 11) | (minutes << 5) | (seconds << 1);
return (date << 16) | time;
}
好極了!謝謝。 – Matt 2012-08-10 23:01:34