欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#时间操作类分享

程序员文章站 2023-11-26 23:20:58
本文实例为大家分享了c#时间操作类的具体代码,供大家参考,具体内容如下 using system; namespace dotnet.utilities...

本文实例为大家分享了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
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。