你真的清楚DateTime in C#吗?

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

日期和时间,在咱们开发中很是重要。DateTime在C#中,专门用来表达和处理日期和时间。html

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

1、什么是DateTime

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

看一下DateTime的定义:数据库

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

从定义能够知道,DateTime实现了IComparableIConvertibleIEquatableIFormattableISerializable。所以,DateTime可让咱们使用有关日期和时间的不少相关信息。c#

    为了防止不提供原网址的转载,特在这里加上原文连接:http://www.javashuo.com/article/p-aydjnjcp-mw.html微信

2、构造

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

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

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

3、静态字段

DateTime包括三个静态字段:this

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);

就完成了转换。

4、方法

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;

5、属性

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数据映射详解

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

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;

7、日期的格式化

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

有这么一个表:

对照这个表就能够:

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

8、阴历

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

扫描二维码,关注我的公众号,能够第一时间获得最新的我的文章和内容推送

本文版权归做者全部,转载请保留此声明和原文连接

相关文章
相关标签/搜索