Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the DateOnly and TimeOnly types of .NET 6

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of how to use the DateOnly and TimeOnly types of. NET 6, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this article on how to use the DateOnly and TimeOnly types of .NET 6. Let's take a look.

Foreword:

Before .NET 6, there was no way to directly represent dates without time (such as birthdays) or times of day without dates (such as alarm clocks).

Although we can use the DateTime class and the TimeSpan class instead, there are some problems in actual use:

Var dateOnly = new DateTime (2021, 10, 1); / / output 2021-10-1 000 new TimeSpan 0000 hand / problem: there is still time, it does not represent the birth at 0 o'clock var timeOnly = new TimeSpan (11,22,0) .add (TimeSpan.FromHours (24)); / / output 1.112200beat / question: 24 hours later, it should still be the current time, the actual number of days with it

Therefore,. Net 6 introduces DateOnly and TimeOnly structures, which can store dates and times

Example 1. Create an instance

We can directly create an instance of DateOnly or TimeOnly:

Var dateOnly = new DateOnly (2021, 10, 1); / / output 10/1/2021var timeOnly = new TimeOnly (11, 22, 0); / / output 11:22 AM

You can also use the FromDateTime method to generate from an DateTime instance:

Var datetime = new DateTime (2021, 10, 1, 10, 1, 0); var dateOnly = DateOnly.FromDateTime (datetime); var timeOnly = TimeOnly.FromDateTime (datetime)

You can also use the FromTimeSpan method to generate TimeOnly from an TimeSpan instance:

Var timeSpan = new TimeSpan (11,22,0); var timeOnly = TimeOnly.FromTimeSpan (timeSpan); 2. Type conversion

Instead, you can use the ToDateTime method to convert the DateOnly instance to DateTime:

Var dateOnly = new DateOnly (2021, 10, 1); var timeOnly = new TimeOnly (11, 22, 0); var datetime = dateOnly.ToDateTime (timeOnly); / / output 10 AM 1 hand 20 21 11:22:00 AM

As you can see, you must bring the TimeOnly instance with you before you can convert it.

You can also use the ToTimeSpan method to convert a TimeOnly instance to TimeSpan:

Var timeOnly = new TimeOnly (11,22,0); var timeSpan = timeOnly.ToTimeSpan (); / / output 11 / 22 var timeSpan 003. Operation

The DateOnly and TimeOnly types also provide comparison operators (for example) and AddXXX methods to operate:

Var noon = new TimeOnly (12,0); if (now < noon) {/ / morning...} dateOnly = dateOnly.AddDays (7); timeOnly = timeOnly.AddHours (24)

It is important to note that only the TimeOnly type supports the-operator to get the TimeSpan interval.

Conclusion:

The introduction of DateOnly and TimeOnly does not mean that DateTime will no longer be useful, just using them in different scenarios.

This is the end of the article on "how to use the DateOnly and TimeOnly types of .NET 6". Thank you for reading! I believe you all have a certain understanding of "how to use the DateOnly and TimeOnly types of .NET 6". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report