当前位置: 移动技术网 > IT编程>开发语言>c# > 详解c# 中的DateTime

详解c# 中的DateTime

2020年08月17日  | 移动技术网IT编程  | 我要评论
日期和时间,在我们开发中非常重要。datetime在c#中,专门用来表达和处理日期和时间。本文算是多年使用datetime的一个总结,包括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可以让我们使用有关日期和时间的很多相关信息。

二、构造

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

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

datetime date1 = new datetime(2020, 7, 14);
datetime date2 = new datetime(2020, 7, 14, 14, 23, 40);
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(30, 0, 0, 0);
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(2020, 7, 13, 14, 20, 10);
datetime date2 = new system.datetime(2020, 7, 14, 14, 25, 40);
datetime date3 = new system.datetime(2020, 7, 14, 14, 25, 40);

timespan diff1 = date2.subtract(date1);
datetime date4 = date3.subtract(diff1);
timespan diff2 = date3 - date2;

datetime date5 = date2 - diff1;

五、属性

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

datetime mydate = new datetime(2020, 7, 14, 14, 23, 40);
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(2020, 7, 14, 14, 23, 40);

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

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

datetime mydate = new datetime(2020, 7, 14, 14, 23, 40, datetimekind.local);
datetime mydate = new datetime(2020, 7, 14, 14, 23, 40, datetimekind.unspecified);

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

看看下面的例子:

datetime mydate = new datetime(2020, 7, 14, 14, 23, 40);

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时间,导致时间上的转换错误。

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

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

datetime date1 = new system.datetime(2020, 7, 14);

timespan timespan = new system.timespan(10, 5, 5, 1);
datetime addresult = date1 + timespan;
datetime substarctresult = date1 - timespan; 

datetime date2 = new datetime(2020, 7, 14);
datetime date3 = new datetime(2020, 7, 15);

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(2020, 06, 24, 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(2020, 5);
bool is_leapday = calendar.isleapday(2020, 5, 26);

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

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 */

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

以上就是详解c# 中的datetime的详细内容,更多关于c# datetime的资料请关注移动技术网其它相关文章!

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网