C#时间操作类分享

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

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

(0)

相关推荐

  • 深入Unix时间戳与C# DateTime时间类型互换的详解

    Unix时间戳最小单位是秒,开始时间为格林威治标准时间1970-01-01 00:00:00ConvertIntDateTime方法的基本思路是通过获取本地时区表示Unixk开始时间,加上Unix时间值(即过去的秒数). ConvertDateTimeInt方法的基本思路是通过刻度数差,再把刻度数转换为秒数,当然要说明的是,我这里返回的是double类型,意义上并非是真正的Unix时间戳格式.要获取真正Unix时间戳的,只获取整数部分就可以了. 复制代码 代码如下: dangranusing S

  • asp.net(C#)实现功能强大的时间日期处理类完整实例

    本文实例讲述了asp.net(C#)实现功能强大的时间日期处理类.分享给大家供大家参考,具体如下: using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts

  • C# string格式的日期时间字符串转为DateTime类型的方法

    方法一:Convert.ToDateTime(string) string格式有要求,必须是yyyy-MM-dd hh:mm:ss 方法二:Convert.ToDateTime(string, IFormatProvider) DateTime dt; DateTimeFormatInfo dtFormat = new System.GlobalizationDateTimeFormatInfo(); dtFormat.ShortDatePattern = "yyyy/MM/dd";

  • C#时间操作类分享

    本文实例为大家分享了C#时间操作类的具体代码,供大家参考,具体内容如下 using System; namespace DotNet.Utilities { /// <summary> /// 时间类 /// 1.SecondToMinute(int Second) 把秒转换成分钟 /// </summary> public class TimeHelper { /// <summary> /// 将时间格式化成 年月日 的形式,如果时间为null,返回当前系统时间 /

  • 史上最全Java8日期时间工具类(分享)

    这是我总结的Java8日期工具类,应该是比较全面的,满足日常开发绝大部分需求,分享给大家,有错误之处,还望大神指教. /** * Java8日期时间工具类 * * @author JourWon * @date 2020/12/13 */ public class LocalDateUtils { /** * 显示年月日时分秒,例如 2015-08-11 09:51:53. */ public static final String DATETIME_PATTERN = "yyyy-MM-dd

  • php实现mysql数据库操作类分享

    复制代码 代码如下: <?php/*数据库操作类*/class Mysql{ private $LocalHost = 'localhost'; private $LoaclUser = 'root'; private $LocalPass = '123456'; private $LocalBase = 'jiangxibaiyi'; private $LocalCode = 'UTF8'; private $PreFix; private $Conn; private $Start    

  • C# FTP操作类分享

    本文实例为大家分享了C# FTP操作类的具体代码,可进行FTP的上传,下载等其他功能,支持断点续传,供大家参考,具体内容如下 FTPHelper using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; namespace ManagementProjec

  • C#文件操作类分享

    本文实例为大家分享了C#文件操作类的具体代码,供大家参考,具体内容如下 using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Reflection; using System.Collections; using System.Data.Common; namespace DotNet.Utilities { //JSON转换类 public class

  • C#配置文件操作类分享

    C#配置文件操作类,供大家参考,具体内容如下 注意添加引用:System.Configuration: using System; using System.Collections.Generic; using System.Text; using System.Configuration; namespace DotNet.Utilities.配置文件操作类 { public class ConfigHelper_sufei { /// <summary> /// 根据Key取Value值

  • c#注册表操作类分享

    复制代码 代码如下: /// <summary>/// 注册表基项静态域/// /// 主要包括:/// 1.Registry.ClassesRoot 对应于HKEY_CLASSES_ROOT主键/// 2.Registry.CurrentUser 对应于HKEY_CURRENT_USER主键/// 3.Registry.LocalMachine 对应于 HKEY_LOCAL_MACHINE主键/// 4.Registry.User 对应于 HKEY_USER主键/// 5.Registry.

  • PHP中使用Memache作为进程锁的操作类分享

    <?php // 使用Memache 作为进程锁 class lock_processlock{ // key 的前缀 protected $sLockKeyPre; // 重试间隔 protected $iLockRetryInterval; //重试次数 protected $iLockRetryCount; //锁的过期时间 protected $iLockCacheTimeout; // 锁过期后的回调函数 protected $onLockTimeoutFunc; // memache

  • Python SQLite3数据库操作类分享

    接触Python时间也不是很长的,最近有个项目需要分析数据,于是选用Python为编程语言,除了语言特性外主要还是看重Python对于SQLite3数据库良好的支持能力了,因为需要灵活处理大量的中间数据. 刚开始一些模块我还乐此不疲的写SQL语句,后来渐渐厌倦了,回想到以前捣鼓C#的时候利用反射初步构建了个SQL查询构造器,直到发现linq,于是放弃了这个计划,当然微软后来又推出了Entity Framework,这些都是后话了,而且现在我对微软的东西兴趣不是很大的,好了,扯多了,下面继续正文.

  • C# XML操作类分享

    本文实例为大家分享了Android九宫格图片展示的具体代码,供大家参考,具体内容如下 XmlHelper using System.Xml; using System.Data; namespace DotNet.Utilities { /// <summary> /// Xml的操作公共类 /// </summary> public class XmlHelper { #region 字段定义 /// <summary> /// XML文件的物理路径 /// <

随机推荐