欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解c# 中的DateTime

 更新時間:2020年07月16日 15:19:04   作者:Tiger.wang  
這篇文章主要介紹了c# 中的DateTime的相關(guān)資料,文中講解非常細致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下

日期和時間,在我們開發(fā)中非常重要。DateTime在C#中,專門用來表達和處理日期和時間。

本文算是多年使用DateTime的一個總結(jié),包括DateTime對象的整體應(yīng)用,以及如何處理不同的區(qū)域、時區(qū)、格式等內(nèi)容。

一、什么是DateTime

跟我們想的不一樣,DateTime不是一個類(class),而是一個結(jié)構(gòu)(struct),它存在于System命名空間下,在Dotnet Core中,處于System.Runtime.dll中。

看一下DateTime的定義:

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

從定義可以知道,DateTime實現(xiàn)了IComparable、IConvertible、IEquatableIFormattable、ISerializable。因此,DateTime可以讓我們使用有關(guān)日期和時間的很多相關(guān)信息。

二、構(gòu)造

初始化一個DateTime對象,C#提供了11種方式進行初始化,根據(jù)需要,可以使用年月日時分秒,以及Ticks

Ticks是C#里一個獨特的定義,它是以公歷0001年1月1日00:00:00.000以來所經(jīng)歷的以100納秒為間隔的間隔數(shù)。我們知道,納秒、微秒、毫秒和秒之間都是1000倍的關(guān)系,所以,1毫秒等于10000Ticks。這個數(shù)字很重要。在C#到Javascript時間轉(zhuǎn)換時,需要很清楚這個對應(yīng)關(guān)系。

DateTime date1 = new DateTime(2020, 7, 14);
DateTime date2 = new DateTime(2020, 7, 14, 14, 23, 40);
DateTime date3 = new DateTime(637303334200000000);

三、靜態(tài)字段

DateTime包括三個靜態(tài)字段:

MinValue - DateTime的最小值,對應(yīng)公歷0001年1月1日00:00:00.000,Ticks為0;

MaxValue - DateTime的最大值,對應(yīng)公歷9999看12月31日23:59:59.999,Ticks為3155378975999999999;

UnixEpoch - Unix、Javascript下的時間起點,對應(yīng)公歷1970年1月1日00:00:00.000,Ticks為621355968000000000; 

在Javascript中,時間保存的是從UnixEpoch開始,即從1970年1月1日00:00:00.000開始到現(xiàn)在的毫秒數(shù)。所以,C#時間到Javascript時間的轉(zhuǎn)換,可以用以下代碼:

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

就完成了轉(zhuǎn)換。

四、方法

DateTime提供了很多種方法來操作DateTime對象,用于處理諸如向日期添加天數(shù)、小時、分鐘、秒、日期差異、從字符串解析到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提供了年月日時分秒、以及其它一些屬性,用來方便提取日期中的細節(jié)。

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

在大多數(shù)情況下,我們定義時間就直接定義年月日時分秒,例如下面:

DateTime myDate = new DateTime(2020, 7, 14, 14, 23, 40);

這種定義下,這個時間就是Unspecified的。

在使用時,如果應(yīng)用過程中不做時間轉(zhuǎn)換,始終以這種方式用,那不會有任何問題。但在某些情況下,時間有可能會發(fā)生轉(zhuǎn)換,例如跨國應(yīng)用的時間處理,再例如MongoDB,在數(shù)據(jù)庫保存數(shù)據(jù)時,強制使用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);

否則,在時間類型不確定的情況下,時間轉(zhuǎn)換會出現(xiàn)問題。

看看下面的例子:

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

當(dāng)使用ToLocalTime方法時,Unspecified時間會認為自己是UTC時間,而當(dāng)使用ToUniversalTime時,Unspecified時間又會認為自己是LocalTime時間,導(dǎo)致時間上的轉(zhuǎn)換錯誤。

六、時間對象的加減及比較

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;

七、日期的格式化

日期的格式化是相關(guān)DateTime網(wǎng)上詢問和查找最多的內(nèi)容。

有這么一個表:

對照這個表就可以:

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

八、陰歷

DateTime本身依賴于日歷Calendar類。Calendar是一個抽象類,在System.Globalization命名空間下,也在System.Runtime.dll中。而在Calendar類下面,提供了很多不同類型的日歷。跟我們有關(guān)系的,是中國的陰歷ChineseLunisolarCalendar。

使用也很簡單:

Calendar calendar = new ChineseLunisolarCalendar();

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

嗯嗯,經(jīng)??搓帤v的伙伴們會看出一點問題:今天是陰歷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);

同樣,我們也可以用公歷轉(zhuǎn)陰歷:

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

有沒有發(fā)現(xiàn),微軟實現(xiàn)的功能,比我們想像的要多?

以上就是詳解c# 中的DateTime的詳細內(nèi)容,更多關(guān)于c# datetime的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • C#使用ScrapySharp快速從網(wǎng)頁采集數(shù)據(jù)

    C#使用ScrapySharp快速從網(wǎng)頁采集數(shù)據(jù)

    這篇文章介紹了使用ScrapySharp快速從網(wǎng)頁采集數(shù)據(jù)的方法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • c# this關(guān)鍵字用法代碼詳解

    c# this關(guān)鍵字用法代碼詳解

    在本篇文章里小編給大家整理的是關(guān)于c# this關(guān)鍵字用法以及相關(guān)實例代碼,有興趣的朋友們可以學(xué)習(xí)下。
    2020-02-02
  • C#基于百度AI實現(xiàn)機器翻譯功能

    C#基于百度AI實現(xiàn)機器翻譯功能

    眾所周知,基于百度ai開發(fā)平臺我們可以實現(xiàn)了人臉識別、文字識別 、語音識別等功能。本文將介紹它的另一個功能,即實現(xiàn)機器翻譯,感興趣的可以了解一下
    2022-01-01
  • c#基礎(chǔ)系列之System.String的深入理解

    c#基礎(chǔ)系列之System.String的深入理解

    這篇文章主要給大家介紹了關(guān)于c#基礎(chǔ)系列之System.String的深入理解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-09-09
  • C#使用Process類調(diào)用外部程序分解

    C#使用Process類調(diào)用外部程序分解

    這篇文章主要介紹了C#使用Process類調(diào)用外部程序分解,分別介紹了啟動外部程序、關(guān)掉外部程序、關(guān)掉后調(diào)用一些方法的方法,需要的朋友可以參考下
    2014-07-07
  • C#?PictureBox控件方法參數(shù)及圖片刪除重命名上傳詳解

    C#?PictureBox控件方法參數(shù)及圖片刪除重命名上傳詳解

    這篇文章主要為大家介紹了C#?PictureBox控件方法參數(shù)及圖片刪除重命名上傳示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-08-08
  • Unity3D實現(xiàn)物體閃爍效果

    Unity3D實現(xiàn)物體閃爍效果

    這篇文章主要為大家詳細介紹了Unity3D實現(xiàn)物體閃爍效果,類似霓虹燈、跑馬燈、LED燈效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • C#抓取當(dāng)前屏幕并保存為圖片的方法

    C#抓取當(dāng)前屏幕并保存為圖片的方法

    這篇文章主要介紹了C#抓取當(dāng)前屏幕并保存為圖片的方法,實例分析了C#操作圖片的相關(guān)技巧,非常具有實用價值,需要的朋友可以參考下
    2015-04-04
  • Unity存儲游戲數(shù)據(jù)的多種方法小結(jié)

    Unity存儲游戲數(shù)據(jù)的多種方法小結(jié)

    這篇文章主要介紹了Unity存儲游戲數(shù)據(jù)的幾種方法,在游戲開發(fā)中,存儲游戲數(shù)據(jù)是非常重要的,因為游戲數(shù)據(jù)決定了游戲的各個方面,例如游戲的進度、玩家的成就、游戲的設(shè)置,需要的朋友可以參考下
    2023-02-02
  • C#通過配置文件動態(tài)修改web.config內(nèi)容的操作步驟

    C#通過配置文件動態(tài)修改web.config內(nèi)容的操作步驟

    這篇文章主要介紹了C#通過配置文件動態(tài)修改web.config內(nèi)容的操作步驟,文中通過圖文結(jié)合的方式介紹的非常詳細,對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2024-03-03

最新評論