The common operation instance c DateTime of datetime calculates the time difference
#region DateTime operation public class C3 { //DateTime Common operations public static void Fun1() { // Format: 2012-8-16 11:21:29 Console.WriteLine(" Current time: {0}", DateTime.Now.ToString()); // Format: 2012-8-16 0:00:00 Console.WriteLine(" Date section: {0}", DateTime.Now.Date.ToString()); // Format: 11:21:29 Console.WriteLine(" Time section: {0}", DateTime.Now.ToLongTimeString()); // The time of day on which this instance was obtained. Pretty accurate [in milliseconds] Console.WriteLine("TimeOfDay:{0}", DateTime.Now.TimeOfDay.ToString()); Console.WriteLine(" Take Chinese date display _ Year month day time :{0}", DateTime.Now.ToString("f")); Console.WriteLine(" Take Chinese date display _ years :{0}", DateTime.Now.ToString("y")); Console.WriteLine(" Take Chinese date display _ On the day :{0}", DateTime.Now.ToString("m")); Console.WriteLine(" Take Chinese year month day :{0}", DateTime.Now.ToString("D")); // Take the current time, and the format is: 14 : 24 Console.WriteLine(" Take the current time :{0}", DateTime.Now.ToString("t")); // Take the current time, and the format is: 2003-09-23T14:46:48 Console.WriteLine(" Take the current time :{0}", DateTime.Now.ToString("s")); // Take the current time, and the format is: 2003-09-23 14:48:30Z Console.WriteLine(" Take the current time :{0}", DateTime.Now.ToString("u")); // Take the current time, and the format is: 2003-09-23 14:48 Console.WriteLine(" Take the current time :{0}", DateTime.Now.ToString("g")); // Take the current time, and the format is: Tue, 23 Sep 2003 14:52:40 GMT Console.WriteLine(" Take the current time :{0}", DateTime.Now.ToString("r")); // Get the current time n The date of the day DateTime newDay = DateTime.Now.AddDays(100); Console.WriteLine(newDay.ToString()); Console.WriteLine(" Years: {0}", DateTime.Now.Year.ToString()); Console.WriteLine(" Month: {0}", DateTime.Now.Month.ToString()); Console.WriteLine(" Day: {0}", DateTime.Now.Day.ToString()); Console.WriteLine(" When: {0}", DateTime.Now.Hour.ToString()); Console.WriteLine(" Points: {0}", DateTime.Now.Minute.ToString()); Console.WriteLine(" Second: {0}", DateTime.Now.Second.ToString()); Console.WriteLine(" Ms: {0}", DateTime.Now.Millisecond.ToString()); Console.WriteLine(" Number of timing cycles: {0}", DateTime.Now.Ticks.ToString()); Console.WriteLine(" Weeks: {0}", DateTime.Now.DayOfWeek.ToString()); Console.WriteLine("1 Days of the year: {0}", DateTime.Now.DayOfYear.ToString()); } // Client code public static void MyFun() { //struct Itself is 1 A structure //DateTime dt0 = new DateTime(); DateTime dt1 = new DateTime(2012, 8, 14, 10, 54, 55); DateTime dt2 = new DateTime(2012, 12, 21);//2012-12-21 00:00:00 Console.WriteLine(DateDiff(dt1, dt2)); // How many days have I lived DateTime dt3 = new DateTime(2012, 8, 14, 12, 00, 00); DateTime dt4 = new DateTime(1990, 11, 17, 02, 48, 00);//2012-12-21 00:00:00 Console.WriteLine(" How many days have I lived " + DateDiff(dt4, dt3)); } // Calculate the difference in time public static string DateDiff(DateTime DateTime1, DateTime DateTime2) { string dateDiff = null; TimeSpan ts1 = new TimeSpan(DateTime1.Ticks); TimeSpan ts2 = new TimeSpan(DateTime2.Ticks); TimeSpan ts = ts1.Subtract(ts2).Duration(); dateDiff = ts.Days.ToString() + " day " + ts.Hours.ToString() + " hours " + ts.Minutes.ToString() + " minutes " + ts.Seconds.ToString() + " seconds "; return dateDiff; #region note //C# The use of TimeSpan Calculate the difference between the two times // You can add any number between the two dates 1 Unit of time. //TimeSpan ts = Date1 - Date2; //double dDays = ts.TotalDays;// The number of days with a decimal, for example 1 day 12 The result is 1.5 //int nDays = ts.Days;// Integer days, 1 day 12 Hours or 1 day 20 It turns out to be both 1 #endregion } } #endregion