SoFunction
Updated on 2025-04-04

C# time operation category sharing

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>  /// &lt;param name="Separator"&gt;&lt;/param&gt;
  /// &lt;returns&gt;&lt;/returns&gt;
  public string GetFormatDate(DateTime dt, char Separator)
  {
   if (dt != null &amp;&amp; !())
   {
    string tem = ("yyyy{0}MM{1}dd", Separator, Separator);
    return (tem);
   }
   else
   {
    return GetFormatDate(, Separator);
   }
  }
  /// &lt;summary&gt;
  /// Format the time into the form of time, minute and second. If the time is null, return to the current system time  /// &lt;/summary&gt;
  /// &lt;param name="dt"&gt;&lt;/param&gt;
  /// &lt;param name="Separator"&gt;&lt;/param&gt;
  /// &lt;returns&gt;&lt;/returns&gt;
  public string GetFormatTime(DateTime dt, char Separator) {
   if (dt != null &amp;&amp; !())
   {
    string tem = ("hh{0}mm{1}ss", Separator, Separator);
    return (tem);
   }
   else
   {
    return GetFormatDate(, Separator);
   }
  }
  /// &lt;summary&gt;
  /// Convert seconds into minutes  /// &lt;/summary&gt;
  /// &lt;returns&gt;&lt;/returns&gt;
  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  /// &lt;summary&gt;
  /// Return to the last day of a certain month of a certain year  /// &lt;/summary&gt;
  /// <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 ( &gt;= 1)
    {
     dateDiff = () + "moon" + () + "day";
    }
    else
    {
     if ( &gt; 1)
     {
      dateDiff = () + "Hour ago";
     }
     else
     {
      dateDiff = () + "Minutes ago";
     }
    }
   }
   catch
   { }
   return dateDiff;
  }
  #endregion

  #region Gets a two-date interval  /// &lt;summary&gt;
  /// Get two date intervals  /// &lt;/summary&gt;
  /// <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  /// &lt;summary&gt;
  /// Format date and time  /// &lt;/summary&gt;
  /// <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  /// &lt;summary&gt;
  /// Get random date  /// &lt;/summary&gt;
  /// <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 &gt; System.)
   {
    iTotalSecontds = System.;
   }
   else if (dTotalSecontds &lt; System.)
   {
    iTotalSecontds = System.;
   }
   else
   {
    iTotalSecontds = (int)dTotalSecontds;
   }


   if (iTotalSecontds &gt; 0)
   {
    minTime = time2;
    maxTime = time1;
   }
   else if (iTotalSecontds &lt; 0)
   {
    minTime = time1;
    maxTime = time2;
   }
   else
   {
    return time1;
   }

   int maxValue = iTotalSecontds;

   if (iTotalSecontds &lt;= 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.