在将FILETIME转成DataTime类型时发现有8小时偏差,请大家帮忙看一下,先谢了!
private string FILETIMEtoDataTime(FILETIME time)
{
try
{
IntPtr filetime = Marshal.AllocHGlobal( Marshal.SizeOf(typeof(FILETIME)) );
IntPtr systime = Marshal.AllocHGlobal( Marshal.SizeOf(typeof(SYSTEMTIME)) );
Marshal.StructureToPtr(time,filetime,true);
FileTimeToSystemTime( filetime ,systime);
SYSTEMTIME st = (SYSTEMTIME) Marshal.PtrToStructure(systime,typeof(SYSTEMTIME));
SYSTEMTIME st1 = (SYSTEMTIME) Marshal.PtrToStructure(filetime,typeof(SYSTEMTIME));
int year=int.Parse(st.wYear.ToString());
int Month=int.Parse(st.wMonth.ToString());
int day =int.Parse(st.wDay.ToString());
int Hour =int.Parse(st.wHour.ToString());
int Minut =int.Parse(st.wMinute.ToString());
int Second=int.Parse(st.wSecond.ToString());
DateTime t=new DateTime(year,Month,day,Hour,Minut,Second);
t=t.AddHours(8);
return t.ToString("yyyy-MM-dd HH:mm:ss");
}
catch(Exception ex)
{
string ss=ex.ToString();
return "";
}
}
DateTime t=new DateTime(year,Month,day,Hour,Minut,Second);
t=t.AddHours(8);
return t.ToString("yyyy-MM-dd HH:mm:ss");
这句是为了去掉偏差加上的
直接转换
long _Value = (long)FILETIME.dwHighDateTime << 32 | (long)FILETIME.dwLowDateTime;
System.DateTime.FromFileTimeUtc(_Value );