System.DateTimeKind 的用法

最近在使用SQLite的数据库,发现SQLiteConnection类,有一个属性DateTimeKind

去msdn上找了下资料,http://msdn.microsoft.com/en-us/library/shx7s921(v=vs.110).aspx

写了一个demo加深理解,代码已经上传至https://github.com/chucklu/Test/blob/master/DotNetFrameworkClassLibrary/System/DateTimeKind/DateTimeKindDemo/Program.cs

想学习,如何使用git和github的同学,可以加我的qq群   程序员之家146605007

 /// <summary>
    /// DateTimeKind测试
    /// </summary>
    class Program
    {
        static readonly string dateTimeFormat = "yyyy-MM-dd HH:mm:ss";
        static void Main(string[] args)
        {
            DateTime now = DateTime.Now;
            DateTime utcNow = DateTime.UtcNow;
            Console.WriteLine(string.Format("Local时间{0}", now.ToString(dateTimeFormat)));
            Console.WriteLine(string.Format("UTC时间{0}", utcNow.ToString(dateTimeFormat)));

            Console.WriteLine();

            DateTime tmpTime;
            tmpTime = now.ToUniversalTime();
            Console.WriteLine(string.Format("Local时间转换为UTC时间 {0}", tmpTime.ToString(dateTimeFormat)));

            tmpTime = utcNow.ToLocalTime();
            Console.WriteLine(string.Format("UTC时间转换为Local时间 {0}", tmpTime.ToString(dateTimeFormat)));

            Console.WriteLine();

            //系统时区,为北京时间
            tmpTime = DateTime.SpecifyKind(now,DateTimeKind.Unspecified);
            //未指定时间的话,转换Local的时候,时间默认为UTC的,加上当前系统的时区的时差+8,也就是加上8个小时
            Console.WriteLine(string.Format("Unspecified时间转换为Local时间 {0}", tmpTime.ToLocalTime().ToString(dateTimeFormat)));
            //未指定时间的话,转换UTC的时候,时间默认为Local的,加上当前系统的时区的时差-8,也就是减去8个小时
            Console.WriteLine(string.Format("Unspecified时间转换为UTC时间 {0}", tmpTime.ToUniversalTime().ToString(dateTimeFormat)));

            Console.ReadLine();
        }
    }

   另外还需要吐槽下,SQLite的时间建议按照"yyyy-MM-dd HH:mm:ss"的格式进行插入,其他格式可以参考https://www.sqlite.org/lang_datefunc.html

   bool类型必须按照"1或0"插入,不可以用true和false插入

    

    

虽然明白了DateTimeKind的作用,但是实际使用的时候,发现SQLite貌似不太支持

public DateTimeKind DateTimeKind { public get; public set; }
这个属性,设置和不设置,没有区别

 
原文地址:https://www.cnblogs.com/chucklu/p/4105076.html