In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what is the DateTime of C#". In the daily operation, I believe that many people have doubts about what the DateTime of C# is. The editor consulted all kinds of materials and sorted out a simple and easy-to-use method of operation. I hope it will be helpful to answer the doubt of "what is the DateTime of C#?" Next, please follow the editor to study!
What is DateTime
Unlike what we thought, DateTime is not a class, but a struct, which exists under the System namespace, in Dotnet Core, and in System.Runtime.dll.
Take a look at the definition of DateTime:
Public struct DateTime: IComparable, IComparable, IConvertible, IEquatable, IFormattable, System.Runtime.Serialization.ISerializable
As you can see from the definition, DateTime implements IComparable, IConvertible, IEquatable, IFormattable, ISerializable. Therefore, DateTime allows us to use a lot of relevant information about dates and times.
II. Structure
Initialize a DateTime object, and C# provides 11 ways to initialize it, using year, month, day, hour, second, and Ticks as needed.
Ticks is a unique definition in C#. It is based on the number of intervals experienced in 100ns since January 1st, 2001 in the Gregorian calendar. We know that nanoseconds, microseconds, milliseconds and seconds are all 1000 times larger, so 1 millisecond equals 10000Ticks. This number is very important. This correspondence needs to be clear when converting C # to Javascript time.
DateTime date1 = new DateTime (2020, 7, 14); DateTime date2 = new DateTime (2020, 7, 14, 14, 23, 40); DateTime date3 = new DateTime (637303334200000000); III. Static field
DateTime includes three static fields:
The minimum value of MinValue-DateTime, which corresponds to 0 on January 1, 2001 in the Gregorian calendar.
The maximum value of MaxValue-DateTime, corresponding to the Gregorian calendar 9999. On December 31, 2359 Gregorian 59.999 Ticks is 315537897599999999.
The time starting point under UnixEpoch-Unix and Javascript, which corresponds to the Gregorian calendar January 1st, 1970 00000000000000000000000
In Javascript, time is saved from UnixEpoch, that is, from January 1, 1970 to the present millisecond. So, for the conversion from C# time to Javascript time, you can use the following code:
Public static long ToUnixTicks (this DateTime time) {return (long) TimeSpan.FromTicks (time.Ticks-DateTime.UnixEpoch.Ticks). TotalMilliseconds-TimeZoneInfo.Local.GetUtcOffset (time). Hours * 60 * 1000;}
Introduce time in Javascript:
Var time = new Date () .setTime (unix_ticks)
The conversion is completed.
IV. Methods
DateTime provides a number of ways to manipulate DateTime objects, such as adding days, hours, minutes, seconds, date differences, parsing from strings to datetime objects, getting universal time, and so on. I won't talk about it in detail here. If you need it, you can check the Microsoft documentation. It's very detailed.
Give me a few examples:
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, 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; V, attribute
DateTime provides the year, month, day, hour, second, and other attributes to facilitate the extraction of details in the date.
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 ()
Where DayOfWeek is used to determine what day of the week the date is, it is an enumerated value. Note that by convention, the week begins on Sunday, so 0 means Sunday and 6 means Saturday.
DateTimeKind, which is used to define whether the time represented by the instance is based on local time (LocalTime), UTC time (UTC), or unspecified (Unspecified).
In most cases, we define the time directly by defining the year, month, day, hour, minute and second, such as the following:
DateTime myDate = new DateTime (2020, 7, 14, 14, 23, 40)
By this definition, this time is Unspecified.
In use, if the application process does not do time conversion, always used in this way, there will be no problem. However, in some cases, time conversion may occur, such as time processing for multinational applications, or MongoDB, which enforces the use of UTC time when data is saved in the database. In this case, the processing time must be LocalTime or UTC time:
DateTime myDate = new DateTime (2020, 7, 14, 14, 23, 40, DateTimeKind.Local)
Or
DateTime myDate = new DateTime (2020, 7, 14, 14, 23, 40, DateTimeKind.Unspecified)
Otherwise, there will be problems with time conversion when the time type is uncertain.
Look at the following example:
DateTime myDate = new DateTime (2020, 7, 14, 14, 23, 40); var date1 = myDate.ToLocalTime (); Console.WriteLine (date1.ToString ()); / * 7 Accord 14 Think 2020 22:23:40 PM * / var date2 = myDate.ToUniversalTime (); Console.WriteLine (date2.ToString ()); / * 7Accord 14 Accord 2020 6:23:40 AM * /
When using the ToLocalTime method, the Unspecified time will think of itself as UTC time, while when using ToUniversalTime, Unspecified time will think of itself as LocalTime time, resulting in a time conversion error.
For more information about MongoDB processing time, you can take a look at my other article: detailed explanation of MongoDB via Dotnet Core data Mapping.
VI. Addition, subtraction and comparison of time objects
The addition and subtraction of DateTime time objects and the comparison are very convenient. Look at the example:
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; VII
Date formatting is the most frequently asked and searched content of relevant DateTime on the Internet.
There is a watch like this:
[picture]
You can compare this table with:
Date.ToString ("yyyy-MM-dd HH:mm:ss") 8. Lunar calendar
DateTime itself depends on the calendar Calendar class. Calendar is an abstract class, under the System.Globalization namespace, as well as in System.Runtime.dll. Under the Calendar class, many different types of calendars are provided. What has something to do with us is the Chinese lunar calendar ChineseLunisolarCalendar.
It is also easy to use:
Calendar calendar = new ChineseLunisolarCalendar (); DateTime date = new DateTime (2020, 06, 24, calendar); / * 7tick 14max 2020 00:00:00 AM * /
Mm-hmm, friends who often read the lunar calendar will see a problem: today is May 24 of the lunar calendar, why is June 24 written here? This is because today is leap April, so the fifth month of the lunar calendar is actually the sixth month of this lunar year.
So how to tell which month is a leap month?
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)
Similarly, we can also use the Gregorian calendar to transfer to the lunar calendar:
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 * / at this point, the study of "what is the DateTime of C#" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.