/*******************************************************/ /*Project:基础类库 Module :时间函数库 Description : Date : 2008-6-3 15:09:12 Create : Lxc Update : 2015-12-31 TODO : */ /*******************************************************/ namespace EC.Util.Common; /// /// 日期时间操作单元 /// public partial class DateUnit { /// /// 1970-1-1 /// public static DateTime InitDate() { return new DateTime(1970, 1, 1); } /// /// 默认当前日期 /// public static DateTime DefDate() { return DateTime.Now; } /// /// 格式化后的当前日期 /// public static string DefFormatDate() { return FormatDate(DefDate()); } /// ///格式化当前时间 /// public static string DefFormatDateTime() { return FormatDateTime(DefDate()); } /// /// 格式化后的 当前月1号 日期格式yyyy-MM-01 /// public static string DefMonthBegFormat() { return DefDate().ToString("yyyy-MM-01"); } /// /// 字符串格式->格式化为当前日期 /// /// /// public static string FormatDate(string Indate) { DateTime nowDate = DefDate(); if (Indate != null && Indate != "") { nowDate = ToDate(Indate); } return FormatDate(nowDate); } /// /// 格式化为 日期格式yyyy-MM-dd /// /// /// public static string FormatDate(DateTime Indate) { if (!Indate.Equals(null)) { return Indate.ToString("yyyy-MM-dd"); } else { return DefFormatDate(); } } /// /// 格式化为 日期时间格式yyyy-MM-dd HH:mm:ss /// /// /// public static string FormatDateTime(DateTime Indate) { if (!Indate.Equals(null)) { return Indate.ToString("yyyy-MM-dd HH:mm:ss"); } else { return DefFormatDate(); } } /// /// 格式化时间 毫秒 /// /// /// public static string FormatDateTimefff(DateTime Indate) { if (!Indate.Equals(null)) { return Indate.ToString("yyyy-MM-dd HH:mm:ss fff"); } else { return DefFormatDate(); } } #region 时间函数 /// /// 是不是时间格式 /// /// /// public static bool IsDate(string datetm) { bool result = false; try { DateTime dateTime = Convert.ToDateTime(datetm); result = true; } catch { result = false; } return result; } /// /// 将字符串转化成日期,如果输入的字符串不是日期时,则返回1970-1-1 /// /// /// public static DateTime ToDate(string str) { if (str == null || str == "") return DefDate(); else { DateTime dt = DefDate(); if (DateTime.TryParse(str, out dt) == false) return DefDate(); else return dt; } } /// /// 将对象转化成日期,如果输入的对象不是日期时,则返回1970-1-1 /// /// /// public static DateTime ToDate(object obj) { DateTime Result = DefDate(); if (!obj.Equals(null)) { try { Result = (DateTime)obj; } catch (Exception ex) { return DefDate(); } //if (DateTime.TryParse(obj.ToString(), out Result) == false) // return DefDate; } return Result; } /// /// 将对象转化成日期,如果输入的对象不是日期时,则返回1900-1-1 /// /// /// public static DateTime ToDate(System.Nullable obj) { if (!obj.Equals(null)) { return (DateTime)obj; } else { return DefDate(); } } /// /// 20070101-->2007-01-01 /// /// /// public static string StrTodate(string Str) { string stryear, strmon, strday; try { stryear = Str.Remove(4, 4); Str = Str.Remove(0, 4); strmon = Str.Remove(2, 2); strday = Str.Remove(0, 2); stryear = stryear + "-" + strmon + "-" + strday; return stryear; } catch { return DefFormatDate(); } } #endregion 时间函数 #region 周计算 /// /// 周几 /// /// /// public static string DayOfWeek(DateTime curDay) { string[] weekdays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" }; string week = weekdays[Convert.ToInt32(curDay.DayOfWeek)]; return week; } /// /// 第几周 /// /// /// public static int WeekOfYear(DateTime curDay) { int firstdayofweek = Convert.ToInt32(Convert.ToDateTime(curDay.Year.ToString() + "-" + "1-1").DayOfWeek); int days = curDay.DayOfYear; int daysOutOneWeek = days - (7 - firstdayofweek); if (daysOutOneWeek <= 0) { return 1; } else { int weeks = daysOutOneWeek / 7; if (daysOutOneWeek % 7 != 0) weeks++; return weeks + 1; } } #region 返回某年某月最后一天 /// /// 返回某年某月最后一天 /// /// 年份 /// 月份 /// 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 返回某年某月最后一天 #endregion 周计算 #region 返回时间差 /// /// 返回时间差 秒 /// /// /// /// public static double DateDiffSeconds(DateTime begDateTime, DateTime endDateTime) { try { TimeSpan ts = endDateTime - begDateTime; return ts.TotalSeconds; } catch (Exception) { return 0; } } /// /// 返回时间差 分钟 /// /// /// /// public static double DateDiffMinutes(DateTime begDateTime, DateTime endDateTime) { try { TimeSpan ts = endDateTime - begDateTime; return ts.TotalMinutes; } catch (Exception) { return 0; } } public static double DateDiffHours(DateTime begDateTime, DateTime endDateTime) { try { TimeSpan ts = endDateTime - begDateTime; return ts.TotalHours; } catch (Exception) { return 0; } } public static double DateDifDays(DateTime begDateTime, DateTime endDateTime) { try { TimeSpan ts = endDateTime - begDateTime; return ts.TotalDays; } catch (Exception) { return 0; } } #endregion 返回时间差 /// /// Unix时间戳 /// /// 毫秒 public static long UnixTime() { System.DateTime time = DateTime.Now; System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0)); long t = (time.Ticks - startTime.Ticks) / 10000; //除10000调整为13位 return t; } /// /// 将c# DateTime时间格式转换为Unix时间戳格式 /// /// 时间 /// 毫秒 public static long DateTimeToUnixTime(System.DateTime time) { System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0)); long t = (time.Ticks - startTime.Ticks) / 10000; //除10000调整为13位 return t; } /// /// 时间戳转为C#格式时间 /// /// /// public static DateTime UnixTimeToDateTime(long timeStamp) { DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); long lTime = timeStamp * 10000; TimeSpan toNow = new TimeSpan(lTime); return dtStart.Add(toNow); } /// /// Unix时间戳 时间格式化 为日期 /// /// /// public static string FormatDateUnixTime(long timeStamp) { DateTime dtStart = UnixTimeToDateTime(timeStamp); return FormatDate(dtStart); } /// /// Unix时间戳 时间格式化 为时间 /// /// /// public static string FormatDateTimeUnixTime(long timeStamp) { DateTime dtStart = UnixTimeToDateTime(timeStamp); return FormatDateTime(dtStart); } }