using System; using System.Collections.Generic; using System.ComponentModel; using System.Reflection; namespace iWareCommon.Utils { /// /// 日期处理类 /// public class DateTimeHelper { /// /// 返回NULL /// /// public static DateTime? GetNull() { return null; } /// /// 计算两个时间的时间差,返回秒数 /// /// /// /// /// public static int GetTimeDiffer(DateTime? startTime, DateTime? endTime, ref string msg) { if (startTime == null || endTime == null) { return 0; } TimeSpan ts = ((DateTime)endTime - (DateTime)startTime); msg = ""; if (ts.Days != 0) { msg += ts.Days + "天"; } if (ts.Hours != 0) { msg += ts.Hours + "小时"; } if (ts.Minutes != 0) { msg += ts.Minutes + "分钟"; } if (ts.Seconds != 0) { msg += ts.Seconds + "秒"; } return (int)ts.TotalSeconds; } /// /// 转换日期格式为字符串 /// /// /// public static string ConvertToString(DateTime? dt) { if (dt == null) { return string.Empty; } return Convert.ToDateTime(dt).ToString("yyyy-MM-dd HH:mm:ss"); } /// /// 转换日期格式为字符串 /// /// /// public static string ConvertToStringForOnlyShowTime(DateTime? dt) { if (dt == null) { return string.Empty; } return Convert.ToDateTime(dt).ToString("HH:mm:ss"); } } }