Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the time type of C #

Shulou Source: shulou.com Published: 2022-06-02 09:09:37 11月08日 Update

This article is about what the C# time type is. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

C # time type is represented as datetime type

Define a datetime variable dt as follows: DateTimedt;//DateTime is the declaration of the C# time type, and dt is the name of the declared variable

One of the common mistakes to note is assigning a string to a datetime variable, for example, I want the defined variable dt to represent the 20:00:00 time. If written as dt= "20:00:00"; or dt= (DateTime) ("20:00:00")

This is all wrong because "20:00:00" here is a string, or string, and cannot be assigned to dt in the above way. The correct assignment method is: dt=Convert.ToDateTime ("20:00:00")

This assigns the time "20:00:00" to dt

Let's talk about the basics of the C# time type DateTime:

DateTimecurrentTime=newDateTime (); / / create a DateTime type variable currentTime,DateTime is the type and currentTime is the variable name

CurrentTime=System.DateTime.Now;// takes the current time: year, month, day, hour, minute and second

Int year = currentTime.Year;// current year (variable name can be in Chinese)

Int month = currentTime.Month;// current month

Int day = currentTime.Day;// current day

Int = currentTime.Hour;// current tense

Int score = currentTime.Minute;// current score

Int second = currentTime.Second;// current second

Int millisecond = currentTime.Millisecond;// current millisecond DateTimedt1=DateTime.Now

StringWeek=dt1.DayOfWeek;// indicates what day the current date is, and the return value is string (sunday,monday.)

StringDay=dt1.Day;// indicates that the current date is the day of the month, and the return value is int.

/ / there are other methods such as DayOfYear.

A simple function to judge two time intervals, with three parameters: DateTime1 and DateTime2 are the time to be compared, and Type is the unit of comparison. The C # time type function is as follows:

PrivatedoubleDateDiff (DateTimeDateTime1,DateTimeDateTime2,stringType)... {doubledateDiff=0; TimeSpants1=newTimeSpan (DateTime1.Ticks); TimeSpants2=newTimeSpan (DateTime2.Ticks); TimeSpants=ts1.Subtract (ts2). Duration (); switch (Type)... {case "d": dateDiff=ts.TotalDays; break; case "h": dateDiff=ts.TotalHours; break; case "m": dateDiff=ts.TotalMinutes; break; case "s": dateDiff=ts.TotalSeconds; break;} returndateDiff;}

For example, if the DateTime1 is 9 DateTime1 20 and the date time 2 is 10:30:20, call the function DateTimedt1=Convert.ToDateTime ("9:20:00")

DateTimedt2=Convert.ToDateTime ("10:30:20"); doubleday=DateDiff (dt1,dt2, "d"); doublehour=DateDiff (dt1,dt2, "h"); doublemin=DateDiff (dt1,dt2, "m"); doublesecond=DateDiff (dt1,dt2, "s"); day=0.04884259259259259;hour=1.1722222222222223;min=70.333333333333329;second=4220.0; available thank you for reading! This is the end of this article on "what is the time type of C#". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Tags: Time type variable Category function content method date more knowledge article error good practical three two unit reason parameter name Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia OPPO Reno Xiaomi Microsoft Shulou Technology