首頁 > 後端開發 > C#.Net教程 > C# 中的日期時間

C# 中的日期時間

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
發布: 2024-09-03 15:16:12
原創
558 人瀏覽過

在 C# 中,DateTime 是一個結構體。因此它是值類型並用於表示瞬時時間。它用於表示一天中的日期和時間。 DateTime 類型的值範圍為 0001 年 1 月 1 日午夜 12:00:00 至 9999 年 12 月 31 日晚上 11:59:59。 DateTime 的值不能為 null,因為它是值型別。要初始化 DateTime 值,我們可以呼叫 DateTime 建構函式的任何重載。我們也可以將從屬性或方法傳回的值指派給 DateTime 物件。

文法:

以下是初始化 DateTime 結構的新實例的語法:

1

DateTime date_time = new DateTime();

登入後複製

這裡,date_time 是為 DateTime 類型的實例指定的使用者定義名稱。我們使用“new”運算符初始化了該實例。在上面的語法中,我們使用隱式無參數建構函式將 DateTime 初始化為其預設值。我們也可以使用 DateTime 建構函式的任何重載來初始化 DateTime 實例。

DateTime 在 C# 中如何運作?

在 C# 中,我們可以使用 DateTime 並透過多種方式為 DateTime 變數賦值。

  • 我們可以透過呼叫 DateTime 建構函數的任何重載來為 DateTime 變數賦值,無論是無參數建構子或帶有參數的建構函數,如下所示:

1

DateTime dateTime = new DateTime(2020, 2, 8, 7, 32, 56);

登入後複製

上述語句初始化特定年、月、日、小時、分鐘和秒的 DateTime 結構的新實例。

  • 在這裡,我們使用了以下 DateTime 結構的建構子:

1

public DateTime(int year, int month, int day, int hour, int minute, int second);

登入後複製
  • 除了上述建構函數之外,還有其他 10 個可用於 DateTime 的建構函數,如下所示:

1

public DateTime(long ticks);

登入後複製
  • 這裡,「ticks」表示日期和時間,以公曆 0001 年 1 月 1 日 00:00:00.000 起經過的百納秒間隔數表示。

1

public DateTime(long ticks, DateTimeKind kind);

登入後複製
  • 這裡,‘kind’表示枚舉值中的一個值,它表示刻度是否指定本地時間、協調世界時間或以上均不指定。

1

public DateTime(int year, int month, int day);

登入後複製
  • 在此上下文中,「年」代表 1 到 9999 之間的值,「月」代表 1 到 12 之間的值,「日」代表特定月份中的天數範圍內的值。

1

public DateTime(int year, int month, int day, Calendar calendar);

登入後複製
  • 這裡的「calendar」代表用來解釋年、月、日的日曆。

1

public DateTime(int year, int month, int day, int hour, int minute, int second, DateTimeKind kind);

登入後複製

1

public DateTime(int year, int month, int day, int hour, int minute, int second, Calendar calendar);

登入後複製

1

public DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond);

登入後複製
  • 這裡,『毫秒』代表0到999之間的毫秒。

1

public DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, DateTimeKind kind);

登入後複製

1

public DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, Calendar calendar);

登入後複製

1

public DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, Calendar calendar, DateTimeKind kind);

登入後複製
  • 我們可以將屬性或方法傳回的 DateTime 值指派給 DateTime 變量,如下所示:

1

DateTime dateTime = DateTime.Now;

登入後複製

這會將目前日期和時間指派給 DateTime 變數。

  • 我們可以從字串表示中解析 DateTime 值,並將其指派給 DateTime 變量,如下所示:

1

2

string str = "6/2/2020 9:20:40 AM";

DateTime dateTime = DateTime.Parse(str, System.Globalization.CultureInfo.InvariantCulture);

登入後複製

我們可以使用 Parse()、ParseExact()、TryParse() 和 TryParseExact() 方法執行上述轉換。

C# 中的日期時間範例

以下是如何將字串解析為 DateTime 物件的幾個範例:

範例#1

使用 DateTime 提供的屬性和方法顯示當前日期和時間以及明天的日期和時間的範例:

代碼:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

using System;

using System.IO;

namespace ConsoleApp4

{

class Program

{

public static DateTime GetNextDay()

{

//getting next day using AddDays() method

return DateTime.Now.AddDays(1);

}

public static void Main()

{

//displaying current date and time using 'Now' property of DateTime

Console.WriteLine("Current date and time: {0}", DateTime.Now);

DateTime dateTime = GetNextDay();

Console.WriteLine("Tomorrow date and time: {0}", dateTime);

Console.ReadLine();

}

}

}

登入後複製

輸出:

C# 中的日期時間

範例#2

例如,將使用者輸入的年份作為年份,然後使用 DateTime.IsLeapYear() 方法檢查是否是閏年。

代碼:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

using System;

using System.IO;

namespace ConsoleApp4

{

class Program

{

public static void Main()

{

try

{

//taking year as input from user

Console.WriteLine("Please enter a year");

int year = Convert.ToInt32(Console.ReadLine());

//checking if entered year is a leap year or not

//using DateTime.IsLeapYear() method

Console.WriteLine("\n Using IsLeapYear() method:");

if (DateTime.IsLeapYear(year))

{

Console.WriteLine(year + " is a leap year");

}

else

{

Console.WriteLine(year + " is not a leap year");

}

//checking if entered year is a leap year or not

//using DateTime.DaysInMonth() method

Console.WriteLine("\n Using DaysInMonth() method:");

if (DateTime.DaysInMonth(year, 2) == 29)

{

Console.WriteLine(year + " is a leap year");

}

else

{

Console.WriteLine(year + " is not a leap year");

}

}

catch(Exception ex)

{

Console.WriteLine(ex.Message);

}

Console.ReadLine();

}

}

}

登入後複製

輸出:

C# 中的日期時間

範例#3

例如,我們取得一年中的第一天和最後一天。

代碼:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

using System;

using System.IO;

namespace ConsoleApp4

{

class Program

{

public static void Main()

{

DateTime dateTime = DateTime.Now;

//displaying first day of current year

DateTime firstDay = new DateTime(dateTime.Year, 1, 1);

Console.WriteLine("First day of {0} is {1}", dateTime.Year, firstDay);

//getting first day of next year

DateTime dateTimeNext = new DateTime(dateTime.Year + 1, 1, 1);

//subtracting one day from the first day of next year

//to get the last day of current year

DateTime lastday = dateTimeNext.AddDays(-1);

Console.WriteLine("Last day of {0} is {1}", dateTime.Year, lastday);

Console.ReadLine();

}

}

}

登入後複製

輸出:

C# 中的日期時間

結論

dateTime 結構用於處理日期和時間。它用作儲存日期和時間的資料類型。 DateTime 提供了處理日期和時間的屬性和方法。 DateTime 是結構體,且是一個值型別;它不能為空。

以上是C# 中的日期時間的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
vim c-x c-o 補全出現新的窗口
來自於 1970-01-01 08:00:00
0
0
0
合併HTML與C++:實作HTML與C++的結合
來自於 1970-01-01 08:00:00
0
0
0
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板