• 你真的清楚DateTime in C#吗?


    DateTime,就是一个世界的大融合。

    日期和时间,在我们开发中非常重要。DateTime在C#中,专门用来表达和处理日期和时间。

    本文算是多年使用DateTime的一个总结,包括DateTime对象的整体应用,以及如何处理不同的区域、时区、格式等内容。

    一、什么是DateTime

    跟我们想的不一样,DateTime不是一个类(class),而是一个结构(struct),它存在于System命名空间下,在Dotnet Core中,处于System.Runtime.dll中。

    看一下DateTime的定义:

    public struct DateTime : IComparable, IComparable<DateTime>, IConvertible, IEquatable<DateTime>, IFormattable, System.Runtime.Serialization.ISerializable

    从定义可以知道,DateTime实现了IComparableIConvertibleIEquatableIFormattableISerializable。因此,DateTime可以让我们使用有关日期和时间的很多相关信息。

        为了防止不提供原网址的转载,特在这里加上原文链接:https://www.cnblogs.com/tiger-wang/p/13303360.html

    二、构造

    初始化一个DateTime对象,C#提供了11种方式进行初始化,根据需要,可以使用年月日时分秒,以及Ticks

    Ticks是C#里一个独特的定义,它是以公历0001年1月1日00:00:00.000以来所经历的以100纳秒为间隔的间隔数。我们知道,纳秒、微秒、毫秒和秒之间都是1000倍的关系,所以,1毫秒等于10000Ticks。这个数字很重要。在C#到Javascript时间转换时,需要很清楚这个对应关系。

    DateTime date1 = new DateTime(2020714);
    DateTime date2 = new DateTime(2020714142340);
    DateTime date3 = new DateTime(637303334200000000);

    三、静态字段

    DateTime包括三个静态字段:

    MinValue - DateTime的最小值,对应公历0001年1月1日00:00:00.000,Ticks为0;

    MaxValue - DateTime的最大值,对应公历9999看12月31日23:59:59.999,Ticks为3155378975999999999;

    UnixEpoch - Unix、Javascript下的时间起点,对应公历1970年1月1日00:00:00.000,Ticks为621355968000000000;

    在Javascript中,时间保存的是从UnixEpoch开始,即从1970年1月1日00:00:00.000开始到现在的毫秒数。所以,C#时间到Javascript时间的转换,可以用以下代码:

    public static long ToUnixTicks(this DateTime time)
    {
        return (long)TimeSpan.FromTicks(time.Ticks - DateTime.UnixEpoch.Ticks).TotalMilliseconds - TimeZoneInfo.Local.GetUtcOffset(time).Hours * 60 * 60 * 1000;
    }

    在Javascript中引入时间:

    var time = new Date().setTime(unix_ticks);

    就完成了转换。

    四、方法

    DateTime提供了很多种方法来操作DateTime对象,用于处理诸如向日期添加天数、小时、分钟、秒、日期差异、从字符串解析到datetime对象、获得通用时间等等。这儿就不详细说了,需要了可以查微软文档,很详细。

    给几个例子:

    TimeSpan duration = new System.TimeSpan(30000);
    DateTime newDate1 = DateTime.Now.Add(duration);

    DateTime today = DateTime.Now;
    DateTime newDate2 = today.AddDays(30);

    string dateString = "2020-07-14 14:23:40";
    DateTime dateTime12 = DateTime.Parse(dateString);

    DateTime date1 = new System.DateTime(2020713142010);
    DateTime date2 = new System.DateTime(2020714142540);
    DateTime date3 = new System.DateTime(2020714142540);

    TimeSpan diff1 = date2.Subtract(date1);
    DateTime date4 = date3.Subtract(diff1);
    TimeSpan diff2 = date3 - date2;

    DateTime date5 = date2 - diff1;

    五、属性

    DateTime提供了年月日时分秒、以及其它一些属性,用来方便提取日期中的细节。

    DateTime myDate = new DateTime(2020714142340);
    int year = myDate.Year; 
    int month = myDate.Month;
    int day = myDate.Day;
    int hour = myDate.Hour;
    int minute = myDate.Minute;
    int second = myDate.Second;
    int weekDay = (int)myDate.DayOfWeek;
    string weekString = myDate.DayOfWeek.ToString();

    其中,DayOfWeek,是用来判断日期是星期几的,它是一个枚举值。注意,按照惯例,一周是从周日开始的,所以,0表示周日,6表示周六。

    DateTimeKind,用来定义实例表示的时间是基于本地时间(LocalTime)、UTC时间(UTC)或是不指定(Unspecified)。

    在大多数情况下,我们定义时间就直接定义年月日时分秒,例如下面:

    DateTime myDate = new DateTime(2020714142340);

    这种定义下,这个时间就是Unspecified的。

    在使用时,如果应用过程中不做时间转换,始终以这种方式用,那不会有任何问题。但在某些情况下,时间有可能会发生转换,例如跨国应用的时间处理,再例如MongoDB,在数据库保存数据时,强制使用UTC时间。这种情况下,处理时间就必须采用LocalTimeUTC时间:

    DateTime myDate = new DateTime(2020714142340, DateTimeKind.Local);

    DateTime myDate = new DateTime(2020714142340, DateTimeKind.Unspecified);

    否则,在时间类型不确定的情况下,时间转换会出现问题。

    看看下面的例子:

    DateTime myDate = new DateTime(2020714142340);

    var date1 = myDate.ToLocalTime();
    Console.WriteLine(date1.ToString());
    /* 7/14/2020 22:23:40 PM */

    var date2 = myDate.ToUniversalTime();
    Console.WriteLine(date2.ToString());
    /* 7/14/2020 6:23:40 AM */

    当使用ToLocalTime方法时,Unspecified时间会认为自己是UTC时间,而当使用ToUniversalTime时,Unspecified时间又会认为自己是LocalTime时间,导致时间上的转换错误。

    关于MongoDB处理时间的相关内容,可以去看我的另一个文章:MongoDB via Dotnet Core数据映射详解

    六、时间对象的加减及比较

    DateTime时间对象的加减及比较非常方便。看例子:

    DateTime date1 = new System.DateTime(2020714);

    TimeSpan timeSpan = new System.TimeSpan(10551);
    DateTime addResult = date1 + timeSpan;
    DateTime substarctResult = date1 - timeSpan; 

    DateTime date2 = new DateTime(2020714);
    DateTime date3 = new DateTime(2020715);

    bool isEqual = date2 == date3;

    七、日期的格式化

    日期的格式化是相关DateTime网上询问和查找最多的内容。

    有这么一个表:

    对照这个表就可以:

    date.ToString("yyyy-MM-dd HH:mm:ss");

    八、阴历

    DateTime本身依赖于日历Calendar类。Calendar是一个抽象类,在System.Globalization命名空间下,也在System.Runtime.dll中。而在Calendar类下面,提供了很多不同类型的日历。跟我们有关系的,是中国的阴历ChineseLunisolarCalendar

    使用也很简单:

    Calendar calendar = new ChineseLunisolarCalendar();

    DateTime date = new DateTime(20200624, calendar);
    /* 7/14/2020 00:00:00 AM */

    嗯嗯,经常看阴历的伙伴们会看出一点问题:今天是阴历5月24,为什么这儿写的是6月24呢?这个是因为今天闰4月,所以,阴历5月实际是这一个阴历年的第6个月。

    那如何判断哪个月是否闰月呢?

    Calendar calendar = new ChineseLunisolarCalendar();

    bool is_leapYear = calendar.IsLeapYear(2020);
    bool is_leapMonth = calendar.IsLeapMonth(20205);
    bool is_leapDay = calendar.IsLeapDay(2020526);

    同样,我们也可以用公历转阴历:

    DateTime date = DateTime.Now;

    Calendar calendar = new ChineseLunisolarCalendar();

    int year = calendar.GetYear(date);
    /* 2020 */
    int month = calendar.GetMonth(date);
    /* 6 */
    int day = calendar.GetDayOfMonth(date);
    /* 24 */

    以上就是全部内容了。

    有没有发现,微软实现的功能,比我们想像的要多?

    (全文完)


    微信公众号:老王Plus

    扫描二维码,关注个人公众号,可以第一时间得到最新的个人文章和内容推送

    本文版权归作者所有,转载请保留此声明和原文链接

  • 相关阅读:
    Xpath定位总结
    robotframework运行时后台报错UnicodeDecodeError
    Selenium驱动Microsoft Edge浏览器(基于robotframework框架)的方法
    robotframework自动化测试安装配置
    硬币
    矩阵乘法
    动态规划和凸性优化
    动态规划背包问题--做题小总结
    CSAPP实验attacklab
    信息学奥赛出局?教育部:若提出申请,会认真研究
  • 原文地址:https://www.cnblogs.com/tiger-wang/p/13303360.html
Copyright © 2020-2023  润新知