SoFunction
Updated on 2025-03-07

C# Common operation examples of DateTime (datetime calculates time difference)


#region DateTime Operation

    public class C3
    {
// Common operations of DateTime
        public static void Fun1()
        {
//Format: 2012-8-16 11:21:29
("Current time: {0}", ());

//Format: 2012-8-16 0:00:00
("Date part: {0}", ());

//Format: 11:21:29
("Time part: {0}", ());

//Get the time of the day when this instance is obtained. Quite precise [to milliseconds]
            ("TimeOfDay:{0}", ());

("Take the Chinese date to display_year, month, day, time and division: {0}", ("f"));

("Get the Chinese date to display_year and month: {0}", ("y"));

("Take the Chinese date to display_month and date: {0}", ("m"));

("Take Chinese year, month, date: {0}", ("D"));

//Take the current time and division, the format is: 14:24
("Take the current time division: {0}", ("t"));

//Please the current time, the format is: 2003-09-23T14:46:48
("Take the current time division: {0}", ("s"));

//Please the current time, the format is: 2003-09-23 14:48:30Z
("Take the current time division: {0}", ("u"));

//Take the current time, the format is: 2003-09-23 14:48
("Take the current time division: {0}", ("g"));

//Fetch the current time, format: Tue, 23 Sep 2003 14:52:40 GMT
("Take the current time division: {0}", ("r"));

//Get the current time n days after the date time
            DateTime newDay = (100);
            (());

("Year: {0}", ());
("Moon: {0}", ());
("Day: {0}", ());
("Time: {0}", ());
("Score: {0}", ());
("Second: {0}", ());
("milliseconds: {0}", ());

("Number of timing cycles: {0}", ());
("Week: {0}", ());
("How many days of the year: {0}", ());

        }

//Client code
        public static void MyFun()
        {
//struct itself is 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
            (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
("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();
            TimeSpan ts2 = new TimeSpan();

            TimeSpan ts = (ts2).Duration();

dateDiff = () + "day" + () + "hour" + () + "minute" + () + "seconds";
            return dateDiff;

            #region note
//C# uses TimeSpan to calculate the difference between two times
//You can invert any unit of time between two dates.
            //TimeSpan ts = Date1 - Date2;
//double dDays = ;//The number of days with decimals, for example, 1 day and 12 hours result is 1.5
//int nDays = ;//Int nDays = ;//Integer days, 1 day, 12 hours or 1 day, 20 hours, all are 1
            #endregion
        }
    }

    #endregion