C Time Operation Class Sharing

  • 2021-12-21 04:46:16
  • OfStack

This article example for everyone to share the C # time operation class specific code, for your reference, the specific content is as follows


using System;

namespace DotNet.Utilities
{
 /// <summary>
 ///  Time class 
 /// 1 , SecondToMinute(int Second)  Convert seconds into minutes 
 /// </summary>
 public class TimeHelper
 {
  /// <summary>
  ///  Formatting the time as   Year, month, day   Form of , If the time is null Returns the current system time 
  /// </summary>
  /// <param name="dt"> Year, month and day separator </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>
  ///  Formatting the time as   Time and second   Form of , 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>
  ///  Convert seconds into minutes 
  /// </summary>
  /// <returns></returns>
  public static int SecondToMinute(int Second)
  {
   decimal mm = (decimal)((decimal)Second / (decimal)60);
   return Convert.ToInt32(Math.Ceiling(mm));
  }

  #region  Return to the end of a certain year and month 1 Days 
  /// <summary>
  ///  Return to the end of a certain year and month 1 Days 
  /// </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 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() + " Month " + DateTime1.Day.ToString() + " Day ";
    }
    else
    {
     if (ts.Hours > 1)
     {
      dateDiff = ts.Hours.ToString() + " Hours ago ";
     }
     else
     {
      dateDiff = ts.Minutes.ToString() + " Minutes ago ";
     }
    }
   }
   catch
   { }
   return dateDiff;
  }
  #endregion

  #region  Get the interval between two dates 
  /// <summary>
  ///  Get the interval between 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  Formatting date time 
  /// <summary>
  ///  Formatting date time 
  /// </summary>
  /// <param name="dateTime1"> Date time </param>
  /// <param name="dateMode"> Display mode </param>
  /// <returns>0-9 Date of each pattern </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 Year MM Month dd Day ");
    case "4":
     return dateTime1.ToString("MM-dd");
    case "5":
     return dateTime1.ToString("MM/dd");
    case "6":
     return dateTime1.ToString("MM Month dd Day ");
    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

  #region  Get random dates 
  /// <summary>
  ///  Get random dates 
  /// </summary>
  /// <param name="time1"> Start date </param>
  /// <param name="time2"> End date </param>
  /// <returns> Between interval dates   Random date </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);

   //  Gets the number of seconds between two times 
   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
 }
}

Related articles: