using NCC.Dependency;
using System;
using System.Collections;
using System.Data;
using System.Reflection;
using System.Text;
namespace NCC.Common.Extension
{
///
///
///
[SuppressSniffer]
public static class Ext
{
#region Convert
#region 数值转换
///
/// 转换为整型
///
/// 数据
public static int ToInt(this object data)
{
if (data == null)
return 0;
int result;
var success = int.TryParse(data.ToString(), out result);
if (success)
return result;
try
{
return Convert.ToInt32(ToDouble(data, 0));
}
catch (Exception)
{
return 0;
}
}
///
/// 转换为可空整型
///
/// 数据
public static int? ToIntOrNull(this object data)
{
if (data == null)
return null;
int result;
bool isValid = int.TryParse(data.ToString(), out result);
if (isValid)
return result;
return null;
}
///
/// 转换为双精度浮点数
///
/// 数据
public static double ToDouble(this object data)
{
if (data == null)
return 0;
double result;
return double.TryParse(data.ToString(), out result) ? result : 0;
}
///
/// 转换为双精度浮点数,并按指定的小数位4舍5入
///
/// 数据
/// 小数位数
public static double ToDouble(this object data, int digits)
{
return Math.Round(ToDouble(data), digits);
}
///
/// 转换为可空双精度浮点数
///
/// 数据
public static double? ToDoubleOrNull(this object data)
{
if (data == null)
return null;
double result;
bool isValid = double.TryParse(data.ToString(), out result);
if (isValid)
return result;
return null;
}
///
/// 转换为高精度浮点数
///
/// 数据
public static decimal ToDecimal(this object data)
{
if (data == null)
return 0;
decimal result;
return decimal.TryParse(data.ToString(), out result) ? result : 0;
}
///
/// 转换为高精度浮点数,并按指定的小数位4舍5入
///
/// 数据
/// 小数位数
public static decimal ToDecimal(this object data, int digits)
{
return Math.Round(ToDecimal(data), digits);
}
///
/// 转换为可空高精度浮点数
///
/// 数据
public static decimal? ToDecimalOrNull(this object data)
{
if (data == null)
return null;
decimal result;
bool isValid = decimal.TryParse(data.ToString(), out result);
if (isValid)
return result;
return null;
}
///
/// 转换为可空高精度浮点数,并按指定的小数位4舍5入
///
/// 数据
/// 小数位数
public static decimal? ToDecimalOrNull(this object data, int digits)
{
var result = ToDecimalOrNull(data);
if (result == null)
return null;
return Math.Round(result.Value, digits);
}
#endregion
#region 日期转换
///
/// 转换为日期
///
/// 数据
public static DateTime ToDate(this object data)
{
if (data == null)
return DateTime.MinValue;
DateTime result;
return DateTime.TryParse(data.ToString(), out result) ? result : DateTime.MinValue;
}
///
/// 转换为可空日期
///
/// 数据
public static DateTime? ToDateOrNull(this object data)
{
if (data == null)
return null;
DateTime result;
bool isValid = DateTime.TryParse(data.ToString(), out result);
if (isValid)
return result;
return null;
}
#endregion
#region 布尔转换
///
/// 转换为布尔值
///
/// 数据
public static bool ToBool(this object data)
{
if (data == null)
return false;
bool? value = GetBool(data);
if (value != null)
return value.Value;
bool result;
return bool.TryParse(data.ToString(), out result) && result;
}
///
/// 获取布尔值
///
private static bool? GetBool(this object data)
{
switch (data.ToString().Trim().ToLower())
{
case "0":
return false;
case "1":
return true;
case "是":
return true;
case "否":
return false;
case "yes":
return true;
case "no":
return false;
default:
return null;
}
}
///
/// 转换为可空布尔值
///
/// 数据
public static bool? ToBoolOrNull(this object data)
{
if (data == null)
return null;
bool? value = GetBool(data);
if (value != null)
return value.Value;
bool result;
bool isValid = bool.TryParse(data.ToString(), out result);
if (isValid)
return result;
return null;
}
#endregion
///
/// 安全返回值
///
/// 可空值
public static T SafeValue(this T? value) where T : struct
{
return value ?? default(T);
}
#region IsEmpty
///
/// 是否为空
///
/// 值
public static bool IsEmpty(this string value)
{
return string.IsNullOrWhiteSpace(value);
}
///
/// 是否为空
///
/// 值
public static bool IsEmpty(this Guid? value)
{
if (value == null)
return true;
return IsEmpty(value.Value);
}
///
/// 是否为空
///
/// 值
public static bool IsEmpty(this Guid value)
{
if (value == Guid.Empty)
return true;
return false;
}
///
/// 是否为空
///
/// 值
public static bool IsEmpty(this object value)
{
if (value != null && !string.IsNullOrEmpty(value.ToString()))
{
return false;
}
else
{
return true;
}
}
///
/// 判断是否为Null或者空
///
/// 对象
///
public static bool IsNullOrEmpty(this object obj)
{
if (obj == null)
return true;
else
{
string objStr = obj.ToString();
return string.IsNullOrEmpty(objStr);
}
}
#endregion
#region IsNotEmptyOrNull
///
///
///
///
///
public static string ObjToString(this object thisValue)
{
if (thisValue != null) return thisValue.ToString().Trim();
return "";
}
///
///
///
///
///
public static bool IsNotEmptyOrNull(this object thisValue)
{
return ObjToString(thisValue) != "" && ObjToString(thisValue) != "undefined" && ObjToString(thisValue) != "null";
}
#endregion
#endregion
#region DateTime
///
/// 获取格式化字符串,带时分秒,格式:"yyyy-MM-dd HH:mm:ss"
///
/// 日期
/// 是否移除秒
public static string ToDateTimeString(this DateTime dateTime, bool isRemoveSecond = false)
{
if (isRemoveSecond)
return dateTime.ToString("yyyy-MM-dd HH:mm");
return dateTime.ToString("yyyy-MM-dd HH:mm:ss");
}
///
/// 获取格式化字符串,带时分秒,格式:"yyyy-MM-dd HH:mm:ss"
///
/// 日期
/// 是否移除秒
public static string ToDateTimeString(this DateTime? dateTime, bool isRemoveSecond = false)
{
if (dateTime == null)
return string.Empty;
return ToDateTimeString(dateTime.Value, isRemoveSecond);
}
///
/// 获取格式化字符串,不带时分秒,格式:"yyyy-MM-dd"
///
/// 日期
public static string ToDateString(this DateTime dateTime)
{
return dateTime.ToString("yyyy-MM-dd");
}
///
/// 获取格式化字符串,不带时分秒,格式:"yyyy-MM-dd"
///
/// 日期
public static string ToDateString()
{
return DateTime.Now.ToString("yyyy-MM-dd");
}
///
/// 获取格式化字符串,不带时分秒,格式:"yyyy-MM-dd"
///
/// 日期
public static string ToDateString(this DateTime? dateTime)
{
if (dateTime == null)
return string.Empty;
return ToDateString(dateTime.Value);
}
///
/// 获取格式化字符串,不带年月日,格式:"HH:mm:ss"
///
/// 日期
public static string ToTimeString(this DateTime dateTime)
{
return dateTime.ToString("HH:mm:ss");
}
///
/// 获取格式化字符串,不带年月日,格式:"HH:mm:ss"
///
/// 日期
public static string ToTimeString(this DateTime? dateTime)
{
if (dateTime == null)
return string.Empty;
return ToTimeString(dateTime.Value);
}
///
/// 获取格式化字符串,带毫秒,格式:"yyyy-MM-dd HH:mm:ss.fff"
///
/// 日期
public static string ToMillisecondString(this DateTime dateTime)
{
return dateTime.ToString("yyyy-MM-dd HH:mm:ss.fff");
}
///
/// 获取格式化字符串,带毫秒,格式:"yyyy-MM-dd HH:mm:ss.fff"
///
/// 日期
public static string ToMillisecondString(this DateTime? dateTime)
{
if (dateTime == null)
return string.Empty;
return ToMillisecondString(dateTime.Value);
}
///
/// 获取格式化字符串,不带时分秒,格式:"yyyy年MM月dd日"
///
/// 日期
public static string ToChineseDateString(this DateTime dateTime)
{
return string.Format("{0}年{1}月{2}日", dateTime.Year, dateTime.Month, dateTime.Day);
}
///
/// 获取格式化字符串,不带时分秒,格式:"yyyy年MM月dd日"
///
/// 日期
public static string ToChineseDateString(this DateTime? dateTime)
{
if (dateTime == null)
return string.Empty;
return ToChineseDateString(dateTime.SafeValue());
}
///
/// 获取格式化字符串,带时分秒,格式:"yyyy年MM月dd日 HH时mm分"
///
/// 日期
/// 是否移除秒
public static string ToChineseDateTimeString(this DateTime dateTime, bool isRemoveSecond = false)
{
StringBuilder result = new StringBuilder();
result.AppendFormat("{0}年{1}月{2}日", dateTime.Year, dateTime.Month, dateTime.Day);
result.AppendFormat(" {0}时{1}分", dateTime.Hour, dateTime.Minute);
if (isRemoveSecond == false)
result.AppendFormat("{0}秒", dateTime.Second);
return result.ToString();
}
///
/// 获取格式化字符串,带时分秒,格式:"yyyy年MM月dd日 HH时mm分"
///
/// 日期
/// 是否移除秒
public static string ToChineseDateTimeString(this DateTime? dateTime, bool isRemoveSecond = false)
{
if (dateTime == null)
return string.Empty;
return ToChineseDateTimeString(dateTime.Value);
}
///
/// 日期转换为时间戳(时间戳单位秒)
///
///
///
public static long ConvertToTimeStamp(this DateTime time)
{
DateTime Jan1st1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
return (long)(time.AddHours(-8) - Jan1st1970).TotalMilliseconds;
}
///
/// 时间戳转换为日期(时间戳单位秒)
///
///
///
public static DateTime? ConvertToDateTime(this long timeStamp)
{
if (timeStamp == 0)
return null;
var start = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
return start.AddMilliseconds(timeStamp).AddHours(8);
}
#endregion
#region Format
///
/// 获取描述
///
/// 布尔值
public static string Description(this bool value)
{
return value ? "是" : "否";
}
///
/// 获取描述
///
/// 布尔值
public static string Description(this bool? value)
{
return value == null ? "" : Description(value.Value);
}
///
/// 获取格式化字符串
///
/// 数值
/// 空值显示的默认文本
public static string Format(this int number, string defaultValue = "")
{
if (number == 0)
return defaultValue;
return number.ToString();
}
///
/// 获取格式化字符串
///
/// 数值
/// 空值显示的默认文本
public static string Format(this int? number, string defaultValue = "")
{
return Format(number.SafeValue(), defaultValue);
}
///
/// 获取格式化字符串
///
/// 数值
/// 空值显示的默认文本
public static string Format(this decimal number, string defaultValue = "")
{
if (number == 0)
return defaultValue;
return string.Format("{0:0.##}", number);
}
///
/// 获取格式化字符串
///
/// 数值
/// 空值显示的默认文本
public static string Format(this decimal? number, string defaultValue = "")
{
return Format(number.SafeValue(), defaultValue);
}
///
/// 获取格式化字符串
///
/// 数值
/// 空值显示的默认文本
public static string Format(this double number, string defaultValue = "")
{
if (number == 0)
return defaultValue;
return string.Format("{0:0.##}", number);
}
///
/// 获取格式化字符串
///
/// 数值
/// 空值显示的默认文本
public static string Format(this double? number, string defaultValue = "")
{
return Format(number.SafeValue(), defaultValue);
}
///
/// 获取格式化字符串,带¥
///
/// 数值
public static string FormatRmb(this decimal number)
{
if (number == 0)
return "¥0";
return string.Format("¥{0:0.##}", number);
}
///
/// 获取格式化字符串,带¥
///
/// 数值
public static string FormatRmb(this decimal? number)
{
return FormatRmb(number.SafeValue());
}
///
/// 获取格式化字符串,带%
///
/// 数值
public static string FormatPercent(this decimal number)
{
if (number == 0)
return string.Empty;
return string.Format("{0:0.##}%", number);
}
///
/// 获取格式化字符串,带%
///
/// 数值
public static string FormatPercent(this decimal? number)
{
return FormatPercent(number.SafeValue());
}
///
/// 获取格式化字符串,带%
///
/// 数值
public static string FormatPercent(this double number)
{
if (number == 0)
return string.Empty;
return string.Format("{0:0.##}%", number);
}
///
/// 获取格式化字符串,带%
///
/// 数值
public static string FormatPercent(this double? number)
{
return FormatPercent(number.SafeValue());
}
#endregion
#region Hashtable
//
/// 实体转换Hashtable
///
///
///
///
public static Hashtable ToHashtable(this TEntity entity)
{
Hashtable ht = new Hashtable();
PropertyInfo[] ps = entity.GetType().GetProperties();
foreach (PropertyInfo i in ps)
{
ht[i.Name] = i.GetValue(entity, null);
}
return ht;
}
///
/// DataRow 转 HashTable
///
///
///
public static Hashtable ToHashtable(this DataRow dr)
{
Hashtable htReturn = new Hashtable(dr.ItemArray.Length);
foreach (DataColumn dc in dr.Table.Columns)
htReturn.Add(dc.ColumnName, dr[dc.ColumnName]);
return htReturn;
}
#endregion
#region 生成时间戳
///
/// 获取时间戳
///
///
public static string GetTimeStamp
{
get
{
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
return Convert.ToInt64(ts.TotalMilliseconds).ToString();
}
}
///
/// 时间转时间戳
///
public static long TimeToTimeStamp(DateTime dateTime)
{
TimeSpan ts = dateTime - new DateTime(1970, 1, 1, 0, 0, 0);
return Convert.ToInt64(ts.TotalMilliseconds);
}
///
/// 时间戳Timestamp转换成日期
///
///
///
public static DateTime GetDateTime(string timeStamp)
{
try
{
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
long lTime = long.Parse(timeStamp + "0000");
TimeSpan toNow = new TimeSpan(lTime);
DateTime targetDt = dtStart.Add(toNow);
return targetDt;
}
catch (Exception)
{
throw;
}
}
#endregion
#region 时间判断
///
/// 判断时间是否在某个时间段内
///
///
///
///
/// 0-yyyy-MM-dd,1-yyyy-MM,2-yyyy
///
public static bool IsInTimeRange(DateTime nowTime, string beginHm, string endHm, int type = 0)
{
DateTime start = DateTime.Parse(beginHm);
DateTime end = DateTime.Parse(endHm);
switch (type)
{
case 1:
{
var beginTime = DateTime.Parse(beginHm);
var endTime = DateTime.Parse(endHm);
start = new DateTime(beginTime.Year, beginTime.Month, 1, 0, 0, 0, 0);
end = new DateTime(endTime.Year, endTime.Month, DateTime.DaysInMonth(endTime.Year, endTime.Month), 23, 59, 59, 999);
}
break;
case 2:
{
var beginTime = DateTime.Parse(beginHm);
var endTime = DateTime.Parse(endHm);
start = new DateTime(beginTime.Year, 1, 1, 0, 0, 0, 0);
end = new DateTime(endTime.Year, 12, 31, 23, 59, 59, 999);
}
break;
case 0:
{
var beginTime = DateTime.Parse(beginHm);
var endTime = DateTime.Parse(endHm);
start = new DateTime(beginTime.Year, beginTime.Month, beginTime.Day, 0, 0, 0, 0);
end = new DateTime(endTime.Year, endTime.Month, endTime.Day, 23, 59, 59, 999);
}
break;
case 3:
{
var beginTime = DateTime.Parse(beginHm);
var endTime = DateTime.Parse(endHm);
start = new DateTime(beginTime.Year, beginTime.Month, beginTime.Day, beginTime.Hour, beginTime.Minute, beginTime.Second, 0);
end = new DateTime(endTime.Year, endTime.Month, endTime.Day, endTime.Hour, endTime.Minute, endTime.Second, 999);
}
break;
}
if (TimeToTimeStamp(nowTime) >= TimeToTimeStamp(start) && TimeToTimeStamp(nowTime) <= TimeToTimeStamp(end))
{
return true;
}
return false;
}
///
/// 时间判断
///
/// 数据时间
/// 查询开始时间
/// 查询结束时间
///
public static bool timeCalendar(string nowTime, string dayTimeStart, string dayTimeEnd)
{
//设置当前时间
DateTime date = DateTime.Parse(nowTime);
//设置开始时间
DateTime timeStart = DateTime.Parse(dayTimeStart);
timeStart = new DateTime(timeStart.ToDate().Year, timeStart.ToDate().Month, timeStart.ToDate().Day, 0, 0, 0, 0);
//设置结束时间
DateTime timeEnd = DateTime.Parse(dayTimeEnd);
timeEnd = new DateTime(timeEnd.ToDate().Year, timeEnd.ToDate().Month, timeEnd.ToDate().Day, 23, 59, 59, 999);
//当date > timeStart时,date.CompareTo(timeStart)返回 1
//当date = timeStart时,date.CompareTo(timeStart)返回 0
//当date < timeStart时,date.CompareTo(timeStart)返回 -1
if (DateTime.Compare(date, timeStart) >= 0 && DateTime.Compare(date, timeEnd) <= 0)
{
return true;
}
return false;
}
#endregion
///
/// 元转分
///
///
///
public static int GetMoneyYuanToFen(this decimal YuanMoney)
{
return (int)Math.Round(Convert.ToDecimal(YuanMoney) * 100, 0);
}
///
/// 按日期获取年龄
///
///
///
public static int GetAgeByBirthdate(this DateTime birthdate)
{
try
{
DateTime now = DateTime.Now;
int age = now.Year - birthdate.Year;
if (now.Month < birthdate.Month || (now.Month == birthdate.Month && now.Day < birthdate.Day))
{
age--;
}
return age < 0 ? 0 : age;
}
catch (Exception)
{
return 0;
}
}
}
}