This article has shared the specific code of C# time operation for your reference. The specific content is as follows
using System; namespace { /// <summary> /// Time category /// 1. SecondToMinute(int Second) converts seconds into minutes /// </summary> public class TimeHelper { /// <summary> /// Format the time into the form of year, month and day. If the time is null, return to the current system time /// </summary> /// <param name="dt">Year, month, day and separator</param> /// <param name="Separator"></param> /// <returns></returns> public string GetFormatDate(DateTime dt, char Separator) { if (dt != null && !()) { string tem = ("yyyy{0}MM{1}dd", Separator, Separator); return (tem); } else { return GetFormatDate(, Separator); } } /// <summary> /// Format the time into the form of time, minute and second. If the time is null, return to the current system time /// </summary> /// <param name="dt"></param> /// <param name="Separator"></param> /// <returns></returns> public string GetFormatTime(DateTime dt, char Separator) { if (dt != null && !()) { string tem = ("hh{0}mm{1}ss", Separator, Separator); return (tem); } else { return GetFormatDate(, Separator); } } /// <summary> /// Convert seconds into minutes /// </summary> /// <returns></returns> public static int SecondToMinute(int Second) { decimal mm = (decimal)((decimal)Second / (decimal)60); return Convert.ToInt32((mm)); } #region Return to the last day of a certain year and month /// <summary> /// Return to the last day of a certain month of a certain year /// </summary> /// <param name="year">year</param> /// <param name="month">month</param> /// <returns>Day</returns> public static int GetMonthLastDate(int year, int month) { DateTime lastDay = new DateTime(year, month, new ().GetDaysInMonth(year, month)); int Day = ; return Day; } #endregion #region Return time difference public static string DateDiff(DateTime DateTime1, DateTime DateTime2) { string dateDiff = null; try { //TimeSpan ts1 = new TimeSpan(); //TimeSpan ts2 = new TimeSpan(); //TimeSpan ts = (ts2).Duration(); TimeSpan ts = DateTime2 - DateTime1; if ( >= 1) { dateDiff = () + "moon" + () + "day"; } else { if ( > 1) { dateDiff = () + "Hour ago"; } else { dateDiff = () + "Minutes ago"; } } } catch { } return dateDiff; } #endregion #region Gets a two-date interval /// <summary> /// Get two date intervals /// </summary> /// <param name="DateTime1">DateMonday. </param> /// <param name="DateTime2">DateTues. </param> /// <returns>Daily interval TimeSpan. </returns> public static TimeSpan DateDiff2(DateTime DateTime1, DateTime DateTime2) { TimeSpan ts1 = new TimeSpan(); TimeSpan ts2 = new TimeSpan(); TimeSpan ts = (ts2).Duration(); return ts; } #endregion #region Format date time /// <summary> /// Format date and time /// </summary> /// <param name="dateTime1">DateTime</param> /// <param name="dateMode">Display mode</param> /// <returns>Date of 0-9 modes</returns> public static string FormatDate(DateTime dateTime1, string dateMode) { switch (dateMode) { case "0": return ("yyyy-MM-dd"); case "1": return ("yyyy-MM-dd HH:mm:ss"); case "2": return ("yyyy/MM/dd"); case "3": return ("Yyyyy MM month dd day"); case "4": return ("MM-dd"); case "5": return ("MM/dd"); case "6": return ("MM month dd day"); case "7": return ("yyyy-MM"); case "8": return ("yyyy/MM"); case "9": return ("yyyyy year MM month"); default: return (); } } #endregion #region Gets a random date /// <summary> /// Get random date /// </summary> /// <param name="time1">First date</param> /// <param name="time2">End date</param> /// <returns>Random dates between interval dates</returns> public static DateTime GetRandomTime(DateTime time1, DateTime time2) { Random random = new Random(); DateTime minTime = new DateTime(); DateTime maxTime = new DateTime(); ts = new ( - ); // Get the number of seconds between two times double dTotalSecontds = ; int iTotalSecontds = 0; if (dTotalSecontds > System.) { iTotalSecontds = System.; } else if (dTotalSecontds < System.) { iTotalSecontds = System.; } else { iTotalSecontds = (int)dTotalSecontds; } if (iTotalSecontds > 0) { minTime = time2; maxTime = time1; } else if (iTotalSecontds < 0) { minTime = time1; maxTime = time2; } else { return time1; } int maxValue = iTotalSecontds; if (iTotalSecontds <= System.) maxValue = System. + 1; int i = ((maxValue)); return (i); } #endregion } }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.