zoukankan      html  css  js  c++  java
  • C#DateTime.Now取得本地时间不对

    替换方法

    改用windowsAPI获取

           [DllImport("kernel32.dll", EntryPoint = "GetSystemTime", SetLastError = true)]
            internal static extern void GetSystemTime(out SYSTEMTIME st);
            [DllImport("kernel32.dll", EntryPoint = "GetLocalTime", SetLastError = true)]
            internal static extern void GetLocalTime(out SYSTEMTIME st);
    
              /// <summary>
            /// 获取时间戳
            /// </summary>
            /// <returns></returns>
            public static DateTime Now()
            {
                try
                {
                    SYSTEMTIME time;
                    TimeHelper.GetLocalTime(out time);
                    DateTime now = new DateTime(time.wYear, time.wMonth, time.wDay, time.wHour, time.wMinute, time.wSecond);
                    return now;
                }
                catch
                {
                    return DateTime.Now;
                }
               
            }
    
            [StructLayout(LayoutKind.Sequential)]
            public struct SYSTEMTIME
            {
                public short wYear;
                public short wMonth;
                public short wDayOfWeek;
                public short wDay;
                public short wHour;
                public short wMinute;
                public short wSecond;
                public short wMilliseconds;
            }
  • 相关阅读:
    Linux学习之路3-HelloWorld
    Linux学习之路2-linux系统烧写
    Linux学习之路1
    linux常用命令总结
    禅道配置发邮件功能
    SHELVE模块
    PICKLE模块
    JSON_dump和load
    json.dumps和loads方法
    模块调用
  • 原文地址:https://www.cnblogs.com/wllhq/p/13852716.html
Copyright © 2011-2022 走看看