1. Initialization:
DateTime dt = new DateTime(2011, 3, 4); DateTime dt3 = new DateTime(2011, 3, 4, 17, 08, 09); DateTime dt1 = ; DateTime dt2 = ; DateTime dt5 = new DateTime(20000, );
With lunar date
DateTime dt4 = new DateTime(1983, 5, 17, new ()); //Lunar Date(dt4); //1983/6/27 0:00:00
2. Format function.
1. Commonly used
DateTime dt = ; s1=();//2019-06-25 10:38:31 s2=("d");//2019-06-25 s3=().ToString();//2019-06-25 10:38:31 s4=().ToString();//2019-06-25 2:38:31
2. Long, Date and Time
s1 = (); // June 25, 2019s2 = ();// 2019-06-25 s3 = (); // 10:41:14 s4 = ();// 10:41
3. Attributes
1. Date part
s1 = ();//date. 2019-06-25 0:00:00s2 = ();//Year. 2019s3 = ();//moon. 6s4 = ();//day. 25s5 = ();//Week. Tuesdays6 = Convert.ToInt32().ToString(); //2 s7 = ();// What day of the year? 176s8 = ( / 7 + 1).ToString();//What week of the year。26
2. Time part
s1 = ();//time. 10:50:23.4881958s2 = ();// When, 10s3 = ();//min., 50s4 = ();//Second 23s5 = ();//millisecond。 488
3. Time period
Ticks: The number of intervals at intervals of 100 nanoseconds (i.e. nanoseconds) that have passed since midnight on January 1, 0001.
s1 = ();//onetickrepresent100ns,One millisecond=10000tick,One second is equal to107indivualticks.636970570197681958
4. Date operation
TimeSpan contains the following four constructors:
- TimeSpan(Int64): Initialize to the specified number of ticks.
- TimeSpan(Int32, Int32, Int32): Initialize to the specifiedHours, minutes and seconds。
- TimeSpan(Int32, Int32, Int32, Int32): Initialize to the specifiedDays, hours, minutes and seconds。
- TimeSpan(Int32, Int32, Int32, Int32, Int32): Initialize to the specifieddays, hours, minutes, seconds, and milliseconds.
1. Add or subtract how many X:
s1 = (1).ToString();// 2020-06-25 11:01:09 s2 = (-1).ToString();// 2019-05-25 11:01:24 s3 = (1).ToString();// 2019-06-26 11:01:24 s4 = (-1).ToString();// 2019-06-25 10:01:24 s5 = (1).ToString();//2019-06-25 11:02:24 s6 = (1000).ToString();//2019-06-25 11:01:24
Date addition and subtraction TimeSpan:
DateTime dt = ; s1 = dt + (2.1).ToString();//2019-06-25 11:05:392.02:24:00 s2 = ((2.1)).ToString();//2019-06-27 13:29:39 s3 = (dt - new TimeSpan(1, 2, 0, 0, 0)).ToString();// 2019-06-24 9:05:39 s4 = ((2)).ToString();//2019-06-23 11:05:39
2. TimeSpan with two date differences
s1 = (dt - (-3)).();//3 s2 = (((-3))).();//3
3. Compare time
s1 = ((1)).ToString();//-1 s2 = (dt == (1)).ToString();//False s3 = (dt < (1)).ToString();// True
5. Static method:
s1 = (2019, 6).ToString();//How many days are there in a certain month, 30s2 = (2019).ToString(); //Is it a leap year? Falses3 = (dt, new DateTime(2019, 6, 25))) //Compare. 1 s4 = ("2019,5,16").ToString();//2019-05-16 0:00:00 s5 = ("1899-12-30", "yyyy-MM-dd", ).ToString();//1899-12-30 0:00:00 s6 = ("2019,5,16").ToString();//2019-05-16 0:00:00
6. Calculate the week of the year
1. See above.
2. Use GregorianCalendar
GregorianCalendar gc = new GregorianCalendar(); int weekOfYear = (, , ); s1 = ();// 26
7. Get JS-related date number format (Unix timestamp)
JavaScript stores zero time as the number of milliseconds since 00:00:00 UTC (Coordinated Universal Time) on January 1, 1970.
// Turn time into a number, the same value as JS () and three ways to millisecondsvar a = (().Ticks - 621355968000000000) / 10000; var b = (().Ticks - new DateTime(1970, 1, 1).Ticks) / 10000; var c = (Int64)(() - new DateTime(1970, 1, 1)).TotalMilliseconds; //Change the number into time (local time), same as var d= new Date(1000000000000) in JS.var d = ("1970-01-01 00:00:00").AddMilliseconds(c); (a); (b); (c); (()); //Output://1620634282422 //1620634282422 //1620634282422 //2021/5/10 Monday 16:11:22
This is all about this article about the usage of the DateTime function in C#. I hope it will be helpful to everyone's learning and I hope everyone will support me more.