123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace Bowin.Common.DataTime
- {
- /// <summary>
- /// 时间类
- /// </summary>
- public class DataTimeHelper
- {
- #region 将时间格式化成 年月日 的形式,如果时间为null,返回当前系统时间
- /// <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);
- }
- }
- #endregion
- #region 将时间格式化成 时分秒 的形式,如果时间为null,返回当前系统时间
- /// <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);
- }
- }
- #endregion
- #region 把秒转换成分钟
- /// <summary>
- /// 把秒转换成分钟
- /// </summary>
- /// <returns></returns>
- public static int SecondToMinute(int Second)
- {
- decimal mm = (decimal)((decimal)Second / (decimal)60);
- return Convert.ToInt32(Math.Ceiling(mm));
- }
- #endregion
- #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
- #region Excel数字转换时间格式
- /// <summary>
- /// Excel数字转换时间格式
- /// </summary>
- /// <param name="timeStr">数字,如:42583对应文本格式日期2016-08-01 </param>
- /// <returns>日期/时间格式</returns>
- public string ToDateTimeValue(string strNumber)
- {
- if (!string.IsNullOrWhiteSpace(strNumber))
- {
- Decimal tempValue;
- //先检查 是不是数字;
- if (Decimal.TryParse(strNumber, out tempValue))
- {
- //天数,取整
- int day = Convert.ToInt32(Math.Truncate(tempValue));
- //这里也不知道为什么. 如果是小于32,则减1,否则减2
- //日期从1900-01-01开始累加
- // day = day < 32 ? day - 1 : day - 2;
- DateTime dt = new DateTime(1900, 1, 1).AddDays(day < 32 ? (day - 1) : (day - 2));
- //小时:减掉天数,这个数字转换小时:(* 24)
- Decimal hourTemp = (tempValue - day) * 24;//获取小时数
- //取整.小时数
- int hour = Convert.ToInt32(Math.Truncate(hourTemp));
- //分钟:减掉小时,( * 60)
- //这里舍入,否则取值会有1分钟误差.
- Decimal minuteTemp = Math.Round((hourTemp - hour) * 60, 2);//获取分钟数
- int minute = Convert.ToInt32(Math.Truncate(minuteTemp));
- //秒:减掉分钟,( * 60)
- //这里舍入,否则取值会有1秒误差.
- Decimal secondTemp = Math.Round((minuteTemp - minute) * 60, 2);//获取秒数
- int second = Convert.ToInt32(Math.Truncate(secondTemp));
- //时间格式:00:00:00
- string resultTimes = string.Format("{0}:{1}:{2}",
- (hour < 10 ? ("0" + hour) : hour.ToString()),
- (minute < 10 ? ("0" + minute) : minute.ToString()),
- (second < 10 ? ("0" + second) : second.ToString()));
- if (day > 0)
- return string.Format("{0} {1}", dt.ToString("yyyy-MM-dd"), resultTimes);
- else
- return resultTimes;
- }
- }
- return string.Empty;
- }
- #endregion
- }
- }
|