public static string DateTimeFormatStrLine(string date) { DateTime dbNow = CommonUtil.GetDBDateTime(); string year = dbNow.Year.ToString(); string month = dbNow.Month.ToString(); string day = dbNow.Day.ToString(); if (date.Trim().Length == RegexConsts.DATE_FORMAT_DAY.Length && (date.IndexOf("-") < 0 && date.IndexOf("/") < 0)) { year = TryConvertUtil.ToInt(date.Substring(0, 4), dbNow.Year).ToString(); month = TryConvertUtil.ToInt(date.Substring(4, 2), dbNow.Month).ToString().PadLeft(2, '0'); day = TryConvertUtil.ToInt(date.Substring(6, 2), dbNow.Day).ToString().PadLeft(2, '0'); return(string.Format("{0}-{1}-{2} 00:00:00", year, month, day)); } else if (date.Trim().Length == RegexConsts.TIME_FORMAT_SECOND.Length && (date.IndexOf("-") < 0 && date.IndexOf("/") < 0 && date.IndexOf(":") < 0)) { year = TryConvertUtil.ToInt(date.Substring(0, 4), dbNow.Year).ToString(); month = TryConvertUtil.ToInt(date.Substring(4, 2), dbNow.Month).ToString().PadLeft(2, '0'); day = TryConvertUtil.ToInt(date.Substring(6, 2), dbNow.Day).ToString().PadLeft(2, '0'); string hour = TryConvertUtil.ToInt(date.Substring(8, 2), dbNow.Hour).ToString().PadLeft(2, '0'); string minute = TryConvertUtil.ToInt(date.Substring(10, 2), dbNow.Minute).ToString().PadLeft(2, '0'); string second = TryConvertUtil.ToInt(date.Substring(12, 2), dbNow.Second).ToString().PadLeft(2, '0'); return(string.Format("{0}-{1}-{2} {3}:{4}:{5}", year, month, day, hour, minute, second)); } else { return(TryConvertUtil.ToDateTime(date, dbNow).ToString("yyyy-MM-dd HH:mm:ss")); } }
/// <summary> /// 获取上周一、周日日期 /// </summary> /// <returns></returns> public static Tuple <DateTime, DateTime> GetPreWeekZone() { //当前数据库时间 DateTime dbNow = CommonUtil.GetDBDateTime(); //上周一 DateTime preMonday = dbNow.AddDays(1 - TryConvertUtil.ToInt(dbNow.DayOfWeek.ToString("d"))).AddDays(-7).Date; //上周日 DateTime preSunday = preMonday.AddDays(7).Date.AddSeconds(-1); return(Tuple.Create(preMonday, preSunday)); }
/// <summary> /// 获取本周几 /// </summary> /// <param name="day">周几</param> /// <returns>日期</returns> public static DateTime GetCurDayOfWeek(int day) { //当前数据库时间 DateTime dbNow = CommonUtil.GetDBDateTime(); if (day <= 0) { day = 1; } else if (day >= 8) { day = 7; } return(dbNow.AddDays(day - TryConvertUtil.ToInt(dbNow.DayOfWeek.ToString("d"))).Date); }
public static List <TResult> ToList <TResult>(this DataTable dt) where TResult : class, new() { if (dt == null) { return(null); } //创建一个属性的列表 List <PropertyInfo> prlist = new List <PropertyInfo>(); //获取TResult的类型实例 反射的入口 Type t = typeof(TResult); //获得TResult 的所有的Public 属性 并找出TResult属性和DataTable的列名称相同的属性(PropertyInfo) 并加入到属性列表 Array.ForEach <PropertyInfo>(t.GetProperties(), p => { if (dt.Columns.IndexOf(p.Name) != -1) { prlist.Add(p); } }); //创建返回的集合 List <TResult> oblist = new List <TResult>(); foreach (DataRow row in dt.Rows) { //创建TResult的实例 TResult ob = new TResult(); //找到对应的数据 并赋值 prlist.ForEach(p => { if (row[p.Name] != DBNull.Value) { Type dataType = row[p.Name].GetType(); switch (dataType.Name) { case "MySqlDateTime": p.SetValue(ob, TryConvertUtil.ToDateTime(row[p.Name]), null); break; case "Int64": case "Int32": { if (p.PropertyType == typeof(bool)) { p.SetValue(ob, TryConvertUtil.ToInt(row[p.Name]) == 1, null); } else { p.SetValue(ob, TryConvertUtil.ToInt(row[p.Name]), null); } break; } default: p.SetValue(ob, row[p.Name], null); break; } } }); //放入到返回的集合中. oblist.Add(ob); } return(oblist); }