using System; using System.Collections.Specialized; namespace PMS.Plugins.Common { public static class ExtendMethod { /// /// 返回指定类型的时间格式 /// /// /// /// public static string ToDefaultString(this DateTime? dt, string dateFormat = "yyyy-MM-dd") { return dt == null ? "" : ((DateTime)dt).ToString(dateFormat); } /// /// 空对象返回null,防止空引用异常 /// /// /// 空对象返回null public static string ToStringEX(this object obj) { if (obj == null || obj == DBNull.Value) { return string.Empty; } return obj.ToString(); } /// /// 调用string.IsNullOrEmpty()方法 /// /// /// public static bool IsNullOrEmpty(this string s) { return string.IsNullOrEmpty(s); } /// /// 将空字符串转换成null /// /// /// public static string StringIsNull(this string str) { if (str != null) { if (str.Trim() == "") { return null; } } return str; } /// /// 转为枚举类型 /// /// /// public static T ToEnum(this string str) { return (T)Enum.Parse(typeof(T), str); } /// /// 将string转换成Time 如果是""则返回null /// /// 时间 /// 处理后的时间 public static DateTime? StringIsTime(this string time) { DateTime? dateTime = null; if (!time.IsNullOrEmpty())//如果传入的时间不为空 dateTime = DateTime.Parse(time.Trim()); return dateTime; } /// /// 处理字符中的特殊符号,空对象返回'',防止在解析json字符串时出错。 /// /// /// //public static string ToJsonString(this object s) //{ // return JsonHelper.JsonReplace(s.ToStringEX()); //} /// /// 根据索引值获取键下的第一个值。一般用于获取前端序列化表单提交的数据 /// /// 数据集合 /// 索引值 /// public static string FirstValue(this NameValueCollection c, int index) { if (c.GetValues(index).Length > 0) { return c.GetValues(index)[0]; } else { return null; } } /// /// 根据索引值获取键下的第一个值。一般用于获取前端序列化表单提交的数据 /// /// /// 键名 /// public static string FirstValue(this NameValueCollection c, string name) { if (c.GetValues(name) != null && c.GetValues(name).Length > 0) { return c.GetValues(name)[0]; } else { return null; } } /// /// NameValueCollection获取值 /// /// /// 键 /// 指定类型(int,int?,DateTime,DateTime?,long,long?,string) /// public static T FirstValue(this NameValueCollection collection, string name) { string val = collection.FirstValue(name); Type type = typeof(T); string typeString = type.ToString(); object returnVal = null; #region 转换 switch (typeString) { case "System.String": returnVal = val; break; case "System.Nullable`1[System.DateTime]": DateTime dateNull; if (DateTime.TryParse(val, out dateNull) == true) { returnVal = (DateTime?)dateNull; } break; case "System.DateTime": DateTime date; if (DateTime.TryParse(val, out date) == true) { returnVal = date; } else { throw new ArgumentException("无法转换为System.DateTime, val: " + val); } break; case "System.Int32": int valInt; if (int.TryParse(val, out valInt) == true) { returnVal = valInt; } else { throw new ArgumentException("无法转换为System.Int32, val: " + val); } break; case "System.Nullable`1[System.Int32]": int valIntNull; if (int.TryParse(val, out valIntNull) == true) { returnVal = (int?)valIntNull; } break; case "System.Int64": long valLong; if (long.TryParse(val, out valLong) == true) { returnVal = valLong; } else { throw new ArgumentException("无法转换为System.Int64, val: " + val); } break; case "System.Nullable`1[System.Int64]": long valLongNull; if (long.TryParse(val, out valLongNull) == true) { returnVal = valLongNull; } break; default: throw new Exception("请添加类型并实现: " + typeString); } #endregion return (T)returnVal; } /// /// 根据索引值获取全部值已逗号隔开 /// /// /// 键名 /// public static string GetValue(this NameValueCollection c, string name) { if (c.GetValues(name) != null && c.GetValues(name).Length > 0) { string values = ""; for (int i = 0; i < c.GetValues(name).Length; i++) { values += c.GetValues(name)[i] + ","; } return values.Substring(0, values.Length - 1); } else { return null; } } /// /// 数组中是否含有指定值。匹配时忽略大小写。 /// /// /// 要查找的值 /// public static bool ContainValue(this string[] s, string value) { foreach (string v in s) { if (v.ToLower() == value.ToLower()) { return true; } } return false; } #region DateTime类扩展 /// /// 按默认时间格式输出。长格式 /// /// /// /// public static string ToDefaultString1(this DateTime dt, string stringFormat = "yyyy年MM月dd日 HH:mm:ss") { return dt.ToString(stringFormat); } /// /// 按默认时间格式输出。短格式 /// /// /// /// public static string ToDefaultString2(this DateTime dt, string stringFormat = "yyyy年MM月dd日") { return dt.ToString(stringFormat); } /// /// 按默认时间格式输出。数据库格式 /// /// /// /// public static string ToDefaultString3(this DateTime dt, string stringFormat = "yyyy-MM-dd HH:mm:ss") { return dt.ToString(stringFormat); } /// /// 按默认时间格式输出。年月日格式 /// /// /// /// public static string ToDefaultString4(this DateTime dt, string stringFormat = "yyyy-MM-dd") { return dt.ToString(stringFormat); } /// /// 计算年龄 /// /// /// 生日 /// public static int GetAge(this DateTime dt, DateTime birthday) { return dt.Year - birthday.Year; } /// /// 获取时间当天的开始时间。即时分秒都为0 /// /// /// public static DateTime Start(this DateTime dt) { return new DateTime(dt.Year, dt.Month, dt.Day); } /// /// 获取时间当天的最后一刻时间。即时分秒为23:59:59 /// /// /// public static DateTime End(this DateTime dt) { return new DateTime(dt.Year, dt.Month, dt.Day, 23, 59, 59); } /// /// 获取时间当天的最后一刻时间(可空类型)。即时分秒为23:59:59 /// /// /// public static DateTime? End(this DateTime? dt) { if (dt == null) { return null; } return new DateTime(dt.Value.Year, dt.Value.Month, dt.Value.Day, 23, 59, 59); } /// /// 时间转换成时间戳 /// /// /// public static long ToTimeStamp(this DateTime dt) { var utczero = new DateTime(1970, 1, 1); long rs = 0; if (dt <= new DateTime(1970, 1, 1)) { } else { //rs = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000; rs = (long)(dt - utczero).TotalSeconds; } return rs; } /// /// 获取当前日期月份的第一天 /// /// /// public static DateTime GetFirstDateOfMonth(this DateTime dt) { return new DateTime(dt.Year, dt.Month, 1); } /// /// 获取当前日期月份的最后一天 /// /// /// public static DateTime GetLastDateOfMonth(this DateTime dt) { return dt.GetFirstDateOfMonth().AddMonths(1).AddDays(-1); } #endregion } }