最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#使用DateTime獲取日期和時(shí)間的實(shí)現(xiàn)

 更新時(shí)間:2023年11月22日 15:46:56   作者:mr_five567  
在C#中,DateTime類是用來處理日期和時(shí)間的類,它具有許多屬性和方法,用于操作和獲取日期和時(shí)間的不同部分,本文就來介紹一下C#使用DateTime獲取,感興趣的可以了解一下

在C#中,DateTime類是用來處理日期和時(shí)間的類。它具有許多屬性和方法,用于操作和獲取日期和時(shí)間的不同部分。以下是DateTime類的一些常用屬性和方法。

屬性:

1、DateTime.Now:獲取當(dāng)前日期和時(shí)間。

DateTime currentDateTime = DateTime.Now;
Console.WriteLine(currentDateTime);//2023/11/21 21:26:04

假如我們想要獲取當(dāng)前日期的某些數(shù)據(jù),我們還可以使用DateTime.Now相關(guān)的屬性和方法。

具體常用的有一下這些:

屬性

(1)DateTime.Now.Ticks返回自公元1年1月1日午夜以來經(jīng)過的以100納秒為間隔的時(shí)間單位數(shù)。

long ticks = DateTime.Now.Ticks;
Console.WriteLine(ticks);//637799999912345678

(2)DateTime.Now.Year:獲取當(dāng)前日期的年份部分。

int currentYear = DateTime.Now.Year;
Console.WriteLine(currentYear);//2023

(3)DateTime.Now.Month:獲取當(dāng)前日期的月份部分。

int currentMonth = DateTime.Now.Month;
Console.WriteLine(currentMonth);//11

(4)DateTime.Now.Day:獲取當(dāng)前日期的天數(shù)部分。

int currentDay = DateTime.Now.Day;
Console.WriteLine(currentDay);//21

(5)DateTime.Now.DayOfWeek:獲取當(dāng)前日期是星期幾。

DayOfWeek currentDayOfWeek = DateTime.Now.DayOfWeek;
Console.WriteLine(currentDayOfWeek);//Monday

(6)DateTime.Now.DayOfYear:獲取當(dāng)前日期是一年中的第幾天。

int currentDayOfYear = DateTime.Now.DayOfYear;
Console.WriteLine(currentDayOfYear);//325

(7)DateTime.Now.TimeOfDay:獲取當(dāng)前時(shí)間部分。

TimeSpan currentTimeOfDay = DateTime.Now.TimeOfDay;
Console.WriteLine(currentTimeOfDay);//21:42:31.1234567

方法

(1)DateTime.Now.ToString:將當(dāng)前日期時(shí)間對(duì)象轉(zhuǎn)換為字符串表示。

string currentDateTimeString = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
Console.WriteLine(currentDateTimeString);//2023-11-21 21:42:31

(2)DateTime.Now.ToShortDateString:獲取當(dāng)前日期的短日期字符串表示(只包含日期部分)。

string currentDateShortString = DateTime.Now.ToShortDateString();
Console.WriteLine(currentDateShortString);//2023/11/21

(3)DateTime.Now.ToShortTimeString:獲取當(dāng)前時(shí)間的短時(shí)間字符串表示(只包含時(shí)間部分)。

string currentTimeShortString = DateTime.Now.ToShortTimeString();
Console.WriteLine(currentTimeShortString);//21:42

(4)DateTime.Now.AddDays:將指定的天數(shù)添加到當(dāng)前日期中。

DateTime tomorrow = DateTime.Now.AddDays(1);
Console.WriteLine(tomorrow);//2023/11/22 21:42:31

(5)DateTime.Now.AddMonths:將指定的月份數(shù)添加到當(dāng)前日期中。

DateTime nextMonth = DateTime.Now.AddMonths(1);
Console.WriteLine(nextMonth);//2023/12/21 21:42:31

(6)DateTime.Now.AddYears:將指定的年份數(shù)添加到當(dāng)前日期中。

DateTime nextYear = DateTime.Now.AddYears(1);
Console.WriteLine(nextYear);//2024/11/21 21:42:31

(7)DateTime.Now.AddHours:將指定的小時(shí)數(shù)添加到當(dāng)前時(shí)間中。

DateTime newTime = DateTime.Now.AddHours(2);
Console.WriteLine(newTime);//2023/11/21 23:42:31

(8)DateTime.Now.AddMinutes:將指定的分鐘數(shù)添加到當(dāng)前時(shí)間中。

DateTime newTime = DateTime.Now.AddMinutes(15);
Console.WriteLine(newTime);//2023/11/21 21:57:31

2、DateTime.Today:獲取當(dāng)前日期,時(shí)間部分為午夜。

DateTime currentDate = DateTime.Today;
Console.WriteLine(currentDate); //2023/11/21 00:00:00

3、DateTime.Year:獲取日期的年份部分。

DateTime date = new DateTime(2022, 11, 21);
int year = date.Year;
Console.WriteLine(year);//2022

4、DateTime.Month:獲取日期的月份部分。

DateTime date = new DateTime(2023, 12, 21);
int month = date.Month;
Console.WriteLine(month);//12

5、DateTime.Day:獲取日期的天數(shù)部分。

DateTime date = new DateTime(2023, 11, 21);
int day = date.Day;
Console.WriteLine(day);//21

6、DateTime.DayOfWeek:獲取日期是星期幾。

DateTime date = new DateTime(2023, 11, 21);
DayOfWeek dayOfWeek = date.DayOfWeek;
Console.WriteLine(dayOfWeek);//Monday

7、DateTime.DayOfYear:獲取日期是一年中的第幾天。

DateTime date = new DateTime(2023, 11, 21);
int dayOfYear = date.DayOfYear;
Console.WriteLine(dayOfYear);//325

8、DateTime.TimeOfDay:獲取時(shí)間部分。

DateTime dateTime = new DateTime(2023, 11, 21, 9, 30, 0);
TimeSpan timeOfDay = dateTime.TimeOfDay;
Console.WriteLine(timeOfDay);//09:30:00

9、DateTime.Ticks:獲取自公元1年1月1日午夜以來經(jīng)過的時(shí)間刻度數(shù)。

DateTime dateTime = new DateTime(2023, 11, 21, 9, 30, 0);
long ticks = dateTime.Ticks;
Console.WriteLine(ticks);//637737774640000000

10、DateTime.Kind:獲取日期時(shí)間對(duì)象的 DateTimeKind 值,指示其表示的時(shí)間是本地時(shí)間、協(xié)調(diào)世界時(shí) (UTC) 還是未指定的類型。

DateTime dateTime = DateTime.Now;
DateTimeKind kind = dateTime.Kind;
Console.WriteLine(kind);//Local

方法:

1、DateTime.Compare:比較兩個(gè)日期的大小。

DateTime date1 = new DateTime(2023, 11, 21);
DateTime date2 = new DateTime(2023, 11, 22);
int result = DateTime.Compare(date1, date2);
Console.WriteLine(result);//-1

2、DateTime.Equals:檢查兩個(gè)日期是否相等。

DateTime date1 = new DateTime(2023, 11, 21);
DateTime date2 = new DateTime(2023, 11, 21);
bool isEqual = DateTime.Equals(date1, date2);
Console.WriteLine(isEqual);//True

3、DateTime.IsLeapYear:檢查指定的年份是否為閏年。

int year = 2024;
bool isLeapYear = DateTime.IsLeapYear(year);
Console.WriteLine(isLeapYear);//False

4、DateTime.ToString:將日期時(shí)間對(duì)象轉(zhuǎn)換為字符串表示。

DateTime dateTime = new DateTime(2023, 11, 21, 9, 30, 0);
string dateString = dateTime.ToString("yyyy-MM-dd HH:mm:ss");
Console.WriteLine(dateString);//2023-11-21 09:30:00

5、DateTime.TryParse:嘗試將字符串解析為 DateTime 對(duì)象,如果解析成功返回 true,否則返回 false。

string dateString = "2023-11-21";
DateTime date;
bool success = DateTime.TryParse(dateString, out date);
if (success)
{
    Console.WriteLine(date);
}
else
{
    Console.WriteLine("Invalid date format");
}//2023/11/21 00:00:00

6、DateTime.Now.AddDays:將指定的天數(shù)添加到當(dāng)前日期中。

DateTime tomorrow = DateTime.Now.AddDays(1);
Console.WriteLine(tomorrow);//2023/11/22 21:26:04

7、DateTime.Now.AddMonths:將指定的月份數(shù)添加到當(dāng)前日期中。

DateTime nextMonth = DateTime.Now.AddMonths(1);
Console.WriteLine(nextMonth);//2023/12/21 21:26:04

8、DateTime.Now.AddYears:將指定的年份數(shù)添加到當(dāng)前日期中。

DateTime nextYear = DateTime.Now.AddYears(1);
Console.WriteLine(nextYear);//2024/11/21 21:26:04

9、DateTime.Now.AddHours:將指定的小時(shí)數(shù)添加到當(dāng)前時(shí)間中。

DateTime newTime = DateTime.Now.AddHours(2);
Console.WriteLine(newTime);//2023/11/21 23:26:04

10、DateTime.Now.AddMinutes:將指定的分鐘數(shù)添加到當(dāng)前時(shí)間中。

DateTime newTime = DateTime.Now.AddMinutes(15);
Console.WriteLine(newTime);//2023/11/21 21:41:04

以上是一些常用的。

我們看到DateTime和DateTime.Now具有不少相同的屬性和方法。但是它們并不是一樣的,并且它們是代表不同的屬性和方法。

DateTime結(jié)構(gòu)中的屬性,用于獲取給定日期的年份部分。它接受一個(gè)DateTime對(duì)象,并返回該對(duì)象表示的日期的對(duì)應(yīng)屬性。

而DateTime.Now里面是屬性是靜態(tài)屬性,相當(dāng)于它已經(jīng)定義好DateTime對(duì)象為當(dāng)前日期的對(duì)象。

這點(diǎn)需要注意,得根據(jù)自己得需求進(jìn)行相關(guān)調(diào)用。

到此這篇關(guān)于C#使用DateTime獲取日期和時(shí)間的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)C# DateTime獲取日期和時(shí)間內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C#實(shí)現(xiàn)合并多個(gè)word文檔的方法

    C#實(shí)現(xiàn)合并多個(gè)word文檔的方法

    這篇文章主要介紹了C#實(shí)現(xiàn)合并多個(gè)word文檔的方法,是C#針對(duì)Word文檔操作的一個(gè)非常重要的技巧,需要的朋友可以參考下
    2014-09-09
  • C# AutoMapper 使用方法總結(jié)

    C# AutoMapper 使用方法總結(jié)

    這篇文章主要介紹了C# AutoMapper 使用方法,文中講解非常細(xì)致,代碼幫助大家更好的理解學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • C#生成隨機(jī)ArrayList的方法

    C#生成隨機(jī)ArrayList的方法

    這篇文章主要介紹了C#生成隨機(jī)ArrayList的方法,實(shí)例分析了C#中ArrayList的相關(guān)操作技巧,需要的朋友可以參考下
    2015-06-06
  • C#使用log4net的3種調(diào)用方法

    C#使用log4net的3種調(diào)用方法

    log4net是一個(gè)用于記錄日志的開源框架,它是C#中最常用的日志記錄工具之一,本文給大家介紹了C#使用log4net的3種調(diào)用方法,通過圖文和代碼給大家講解的非常詳細(xì),需要的朋友可以參考下
    2024-03-03
  • C#調(diào)用js庫(kù)的方法小結(jié)

    C#調(diào)用js庫(kù)的方法小結(jié)

    本文主要介紹了C#調(diào)用js庫(kù)的方法小結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • ASP.Net動(dòng)態(tài)讀取Excel文件最簡(jiǎn)方法

    ASP.Net動(dòng)態(tài)讀取Excel文件最簡(jiǎn)方法

    本篇文章給大家分享了ASP.Net動(dòng)態(tài)讀取Excel文件最簡(jiǎn)方法,對(duì)此有需要的讀者們參考學(xué)習(xí)下。
    2018-05-05
  • Stream.Write 與 StreamWriter.Write 的不同

    Stream.Write 與 StreamWriter.Write 的不同

    Stream.Write 與 StreamWriter.Write 是我們?cè)谙蛄髦袑憯?shù)據(jù)時(shí),最常用的方法。下面就詳細(xì)講解這兩個(gè)方法。
    2013-04-04
  • WinForm ToolTip使用方法小結(jié)

    WinForm ToolTip使用方法小結(jié)

    這篇文章主要介紹了WinForm ToolTip使用方法小結(jié),對(duì)C#初學(xué)者有一定的借鑒參考價(jià)值,需要的朋友可以參考下
    2014-08-08
  • 詳解TreeView綁定數(shù)據(jù)庫(kù)

    詳解TreeView綁定數(shù)據(jù)庫(kù)

    這篇文章主要演示了TreeView如何與數(shù)據(jù)庫(kù)進(jìn)行綁定
    2015-07-07
  • C#實(shí)現(xiàn)會(huì)移動(dòng)的文字效果

    C#實(shí)現(xiàn)會(huì)移動(dòng)的文字效果

    這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)會(huì)移動(dòng)的文字效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-04-04

最新評(píng)論

城固县| 邳州市| 阿拉善盟| 东宁县| 镇安县| 远安县| 西峡县| 龙川县| 鹿邑县| 平和县| 和龙市| 砀山县| 湘西| 岢岚县| 新密市| 海安县| 犍为县| 敦化市| 新蔡县| 迭部县| 木兰县| 桃江县| 宁化县| 南华县| 台中县| 漠河县| 西城区| 安吉县| 扬州市| 夹江县| 竹山县| 广南县| 鸡西市| 二手房| 香港 | 永安市| 集贤县| 延安市| 宜昌市| 浑源县| 万盛区|