C # Time operations,
Using System; namespace DotNet. utilities {// <summary> // time class // 1. SecondToMinute (int Second) convert seconds to minutes /// </summary> public class TimeHelper {// <summary> /// format the time in the format of year, month, and day. If the time is null, returns the current system time /// </summary> /// <param name = "dt"> year, month, and day separators </param> /// <param name = "Separator"> </param> // <returns> </returns> public string GetFormatDate (DateTime dt, char Separator) {if (dt! = Null &&! Dt. equals (DBNull. value) {string tem = string. format ("yyyy {0} MM {1} dd", Separator, Separator); return dt. toString (tem);} else {return GetFormatDate (DateTime. now, Separator) ;}/// <summary> /// format the time in the format of hour, minute, and second. If the time is null, returns 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 &&! Dt. equals (DBNull. value) {string tem = string. format ("hh {0} mm {1} ss", Separator, Separator); return dt. toString (tem);} else {return GetFormatDate (DateTime. now, Separator );}} /// <summary> /// converts seconds to minutes /// </summary> /// <returns> </returns> public static int SecondToMinute (int Second) {decimal mm = (decimal) Second/(decimal) 60); return Convert. toInt32 (Math. ceiling (mm);} # region returns a year Last day of a month /// <summary> /// return the last day of a month of a 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 System. globalization. gregorianCalendar (). getDaysInMonth (year, month); int Day = lastDay. day; return Day ;}# endregion # region return time difference public static s Tring DateDiff (DateTime DateTime1, DateTime DateTime2) {string dateDiff = null; try {// TimeSpan ts1 = new TimeSpan (DateTime1.Ticks); // TimeSpan ts2 = new TimeSpan (DateTime2.Ticks ); // TimeSpan ts = ts1.Subtract (ts2 ). duration (); TimeSpan ts = DateTime2-DateTime1; if (ts. days> = 1) {dateDiff = DateTime1.Month. toString () + "month" + DateTime1.Day. toString () + "day";} else {if (ts. hours> 1) {dateDif F = ts. hours. toString () + "Hours Ago";} else {dateDiff = ts. minutes. toString () + "Minutes Ago" ;}} catch {} return dateDiff ;} # endregion # region get the interval of two dates /// <summary> /// get the interval of two dates /// </summary> /// <param name = "DateTime1"> date 1. </Param> /// <param name = "DateTime2"> date 2. </Param> /// <returns> date interval TimeSpan. </Returns> public static TimeSpan DateDiff2 (DateTime DateTime1, DateTime DateTime2) {TimeSpan ts1 = new TimeSpan (DateTime1.Ticks); TimeSpan ts2 = new TimeSpan (DateTime2.Ticks ); timeSpan ts = ts1.Subtract (ts2 ). duration (); return ts ;} # endregion # region format the date and time /// <summary> // format the date and time /// </summary> /// <param name = "dateTime1"> date and time </param> /// <param name = "dateMode"> display mode </param> /// <returns> 0-9 Mode date </returns> public static string FormatDate (dateTime dateTime1, string dateMode) {switch (dateMode) {case "0": return dateTime1.ToString ("yyyy-MM-dd"); case "1 ": return dateTime1.ToString ("yyyy-MM-dd HH: mm: ss"); case "2": return dateTime1.ToString ("yyyy/MM/dd"); case "3 ": return dateTime1.ToString ("MM dd, yyyy"); case "4": return dateTime1.ToString ("MM-dd"); case "5 ": return dateTime1.ToString ("MM/dd"); case "6": return dateTime1.ToString ("MM dd"); case "7 ": return dateTime1.ToString ("yyyy-MM"); case "8": return dateTime1.ToString ("yyyy/MM"); case "9 ": return dateTime1.ToString ("yyyy Year MM Month"); default: return dateTime1.ToString ();}} # endregion # obtain a random date from region /// <summary> /// obtain a random date /// </summary> /// <param name = "time1"> Start date </param> /// <param name = "time2"> end date </param> /// random date between <returns> interval dates </returns> public static DateTime GetRandomTime (DateTime time1, dateTime time2) {Random random = new Random (); DateTime minTime = new DateTime (); DateTime maxTime = new DateTime (); System. timeSpan ts = new System. timeSpan (time1.Ticks-time2.Ticks); // returns the number of seconds between two periods. double dTotalSecontds = ts. totalSeconds; int iTotalSecontds = 0; if (dTotalSecontds> System. int32.MaxValue) {iTotalSecontds = System. int32.MaxValue;} else if (dTotalSecontds <System. int32.MinValue) {iTotalSecontds = System. int32.MinValue;} 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. int32.MinValue) maxValue = System. int32.MinValue + 1; int I = random. next (System. math. abs (maxValue); return minTime. addSeconds (I) ;}# endregion }}
TimeHelper