您好,登錄后才能下訂單哦!
本文實例為大家分享了C#時間操作類的具體代碼,供大家參考,具體內容如下
using System; namespace DotNet.Utilities { /// <summary> /// 時間類 /// 1、SecondToMinute(int Second) 把秒轉換成分鐘 /// </summary> public class TimeHelper { /// <summary> /// 將時間格式化成 年月日 的形式,如果時間為null,返回當前系統時間 /// </summary> /// <param name="dt">年月日分隔符</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> /// 將時間格式化成 時分秒 的形式,如果時間為null,返回當前系統時間 /// </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> /// 把秒轉換成分鐘 /// </summary> /// <returns></returns> public static int SecondToMinute(int Second) { decimal mm = (decimal)((decimal)Second / (decimal)60); return Convert.ToInt32(Math.Ceiling(mm)); } #region 返回某年某月最后一天 /// <summary> /// 返回某年某月最后一天 /// </summary> /// <param name="year">年份</param> /// <param name="month">月份</param> /// <returns>日</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 返回時間差 public static string 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() + "月" + DateTime1.Day.ToString() + "日"; } else { if (ts.Hours > 1) { dateDiff = ts.Hours.ToString() + "小時前"; } else { dateDiff = ts.Minutes.ToString() + "分鐘前"; } } } catch { } return dateDiff; } #endregion #region 獲得兩個日期的間隔 /// <summary> /// 獲得兩個日期的間隔 /// </summary> /// <param name="DateTime1">日期一。</param> /// <param name="DateTime2">日期二。</param> /// <returns>日期間隔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 格式化日期時間 /// <summary> /// 格式化日期時間 /// </summary> /// <param name="dateTime1">日期時間</param> /// <param name="dateMode">顯示模式</param> /// <returns>0-9種模式的日期</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("yyyy年MM月dd日"); 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年MM月"); default: return dateTime1.ToString(); } } #endregion #region 得到隨機日期 /// <summary> /// 得到隨機日期 /// </summary> /// <param name="time1">起始日期</param> /// <param name="time2">結束日期</param> /// <returns>間隔日期之間的 隨機日期</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); // 獲取兩個時間相隔的秒數 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 } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。