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> /// <param name="d">要截断的数字</param> /// <param name="s">要保留的小数位数</param> /// <returns>已经截断的数字</returns> public static decimal ToFixed(this decimal d, int s = 2) { /* * decimal sp = Convert.ToDecimal(Math.Pow(10, s)); * * if (d < 0) * return Math.Truncate(d) + Math.Ceiling((d - Math.Truncate(d)) * sp) / sp; * else * return Math.Truncate(d) + Math.Floor((d - Math.Truncate(d)) * sp) / sp;*/ //以下使用四啥五入 var str = d.ToString(); if (str.IndexOf('.') < 0) { return(TryConvertUtil.ToDecimal(string.Concat(str, ".").PadRight(s + 1 + str.Length, '0'))); } else { var strList = str.Split('.'); if (strList.Length > 1) { string lastStr = strList[1]; if (lastStr.Length == 1) { return(TryConvertUtil.ToDecimal(str.PadRight(1 + str.Length, '0'))); } } return(Math.Round(d, s, MidpointRounding.AwayFromZero)); } }
/// <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 TResult ToModel <TResult>(this DataRow row) where TResult : class, new() { if (row == null) { return(null); } DataTable dt = row.Table; //创建一个属性的列表 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>(); //创建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; default: p.SetValue(ob, row[p.Name], null); break; } } }); //放入到返回的集合中. return(ob); }
public static string DateTimeFormatLine(string date) { return(TryConvertUtil.ToDateTime(date, CommonUtil.GetDBDateTime()).ToString("yyyy-MM-dd")); }
/// <summary> /// 日期格式化,转换失败则返回当前日期 /// </summary> /// <param name="date"></param> /// <returns></returns> public static string DateTimeFormat(string date) { return(TryConvertUtil.ToDateTime(date, CommonUtil.GetDBDateTime()).ToString("yyyy年MM月dd日")); }
/// <summary> /// 获得数据库服务器当前的日期和时间 /// </summary> /// <returns>数据库服务器当前的日期和时间</returns> public static DateTime GetDBDateTime() { return(TryConvertUtil.ToDateTime(DbUtil.DataManager.Current.IData.ExecuteScalar("SELECT NOW()"))); }
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); }