using System; using System.Text; namespace Nirvana.Common { public static partial class Ext { /// /// 获取格式化字符串,带时分秒,格式:"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 DateTime ToDatetimeFromTimeStamp(this string timeStamp) { var ltime = long.Parse(timeStamp); if (timeStamp.Length == 10) { var dto = DateTimeOffset.FromUnixTimeSeconds(ltime); return dto.ToLocalTime().DateTime; } else { var dto = DateTimeOffset.FromUnixTimeMilliseconds(ltime); return dto.ToLocalTime().DateTime; } } /// /// 时间戳转时间 /// /// /// public static DateTime ToDatetimeFromTimeStamp(this long timeStamp, int type = 1) { if (timeStamp <= 0) return DateTime.Now; if (type == 2) { var dto1 = DateTimeOffset.FromUnixTimeMilliseconds(timeStamp); return dto1.ToLocalTime().DateTime; } var dto = DateTimeOffset.FromUnixTimeSeconds(timeStamp); return dto.ToLocalTime().DateTime; } /// /// 时间转时间戳 /// /// /// public static long GetTimeStamp(this DateTime? time) { if (!time.HasValue) return 0; DateTime dateTimeStart = TimeZoneInfo.ConvertTimeToUtc(new DateTime(1970, 1, 1)); long t = (time.Value.Ticks - dateTimeStart.Ticks) / 10000;//除10000调整为13位 return t; } /// /// 出生年月转为年龄 /// /// /// public static int ToAge(this DateTime birthdate, DateTime? nowtime = null) { DateTime now = nowtime.IsEmpty() ? DateTime.Now : nowtime.Value.Date; 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; } /// /// 岁数,小于16岁的显示几岁几个月,其他只显示整岁 /// /// /// public static string ToAAge(this DateTime birthdate) { DateTime now = DateTime.Now; int age = now.Year - birthdate.Year; if ( //当前月份小于出生月份,则不满足一年 now.Month < birthdate.Month //当前月份等于出生月份并且当前天小于出生天,则不满足一年 || (now.Month == birthdate.Month && now.Day < birthdate.Day)) { age--; } age = age < 0 ? 0 : age; int month = now.Month - birthdate.Month; return month <= 0 ? $"{age}岁" : (age >= 16 ? $"{age}岁" : $"{age}岁{month}个月"); } /// /// 岁数 /// /// /// public static string TomAge(this DateTime birthdate) { double month = ToDMonth(birthdate); if (month < 1) { int day = ToBirthDay(birthdate); if (day <= 0) { return string.Empty; } return $"{day}天"; } if (month > 0 && month < 12) { return $"{month.ToInt()}个月"; } double age = month / (12 * 1.0); month = month % (12 * 1.0); return month < 1 || age > 16 ? $"{age.ToInt()}岁" : $"{age.ToInt()}岁{month.ToInt()}个月"; //DateTime now = DateTime.Now; //int age = now.Year - birthdate.Year; //if ( // //当前月份小于出生月份,则不满足一年 // now.Month < birthdate.Month // //当前月份等于出生月份并且当前天小于出生天,则不满足一年 // || (now.Month == birthdate.Month && now.Day < birthdate.Day)) //{ // age--; //} //age = age < 0 ? 0 : age; //int month = now.Month - birthdate.Month; //return month <= 0 ? $"{age}岁" : $"{age}岁{month}个月"; } /// /// 岁数 /// /// 月龄 /// public static string TomAge(this int month, int type = 1) { int age = month / 12; int m = month % 12; if (type == 2) { return $"{age}"; } else if (type == 3) { return m <= 0 ? $"{age}" : $"{age}.{m}"; } return m <= 0 ? $"{age}岁" : $"{age}岁{m}个月"; } /// /// 相差的天数 /// /// /// public static int ToBirthDay(this DateTime birthdate) { DateTime now = DateTime.Now; TimeSpan sp = now.Subtract(birthdate); return sp.Days; } /// /// 相差的月份,返回带小数点的 /// /// /// public static double ToDMonth(this DateTime from) { DateTime to = DateTime.Now; if (to.Ticks < from.Ticks) { DateTime temp = from; from = to; to = temp; } double percFrom = (double)from.Day / DateTime.DaysInMonth(from.Year, from.Month); double percTo = (double)to.Day / DateTime.DaysInMonth(to.Year, to.Month); double months = (to.Year * 12 + to.Month) - (from.Year * 12 + from.Month); double month = months - percFrom + percTo; return month; } /// /// 相差的月份 /// /// /// public static int ToMonth(this DateTime from) { DateTime to = DateTime.Now; if (to.Ticks < from.Ticks) { DateTime temp = from; from = to; to = temp; } double percFrom = (double)from.Day / DateTime.DaysInMonth(from.Year, from.Month); double percTo = (double)to.Day / DateTime.DaysInMonth(to.Year, to.Month); double months = (to.Year * 12 + to.Month) - (from.Year * 12 + from.Month); int month = (int)(months - percFrom + percTo); return month; //int age = now.Year - birthdate.Year; //if ( // //当前月份小于出生月份,则不满足一年 // now.Month < birthdate.Month // //当前月份等于出生月份并且当前天小于出生天,则不满足一年 // || (now.Month == birthdate.Month && now.Day < birthdate.Day)) //{ // age--; //} //age = age < 0 ? 0 : age; //int month = now.Month - birthdate.Month; //return month <= 0 ? age * 12 : age * 12 + month; } /// /// 相差的月份 /// /// 出生年月 /// 是否精确到周,false-否 /// public static decimal ToMonth(this DateTime birthdate, bool IsWeek = false) { //DateTime now = DateTime.Now; //int age = now.Year - birthdate.Year; //if ( // //当前月份小于出生月份,则不满足一年 // now.Month < birthdate.Month // //当前月份等于出生月份并且当前天小于出生天,则不满足一年 // || (now.Month == birthdate.Month && now.Day < birthdate.Day)) //{ // age--; //} //age = age < 0 ? 0 : age; //decimal month = 0; //if (IsWeek && age == 0) //{ // month = (((now - birthdate).TotalDays / 7) / 4.0).ToDecimal(2); //} //else //{ // month = now.Month now.Month - birthdate.Month; //} //return month <= 0 ? age * 12 : (month < 1 ? month : age * 12 + month); DateTime to = DateTime.Now; if (to.Ticks < birthdate.Ticks) { DateTime temp = birthdate; birthdate = to; to = temp; } double percFrom = (double)birthdate.Day / DateTime.DaysInMonth(birthdate.Year, birthdate.Month); double percTo = (double)to.Day / DateTime.DaysInMonth(to.Year, to.Month); double months = (to.Year * 12 + to.Month) - (birthdate.Year * 12 + birthdate.Month); decimal month = (months - percFrom + percTo).ToDecimal(2); return month; } /// /// 相差的月份 /// /// /// 开始时间 /// public static int ToMonth(this DateTime birthdate, DateTime? starttime = null) { DateTime now = DateTime.Now; if (starttime.HasValue) { now = starttime.Value; } int age = now.Year - birthdate.Year; if ( //当前月份小于出生月份,则不满足一年 now.Month < birthdate.Month //当前月份等于出生月份并且当前天小于出生天,则不满足一年 || (now.Month == birthdate.Month && now.Day < birthdate.Day)) { age--; } age = age < 0 ? 0 : age; int month = now.Month - birthdate.Month; return month <= 0 ? age * 12 : age * 12 + month; } public static long GetTimeStamp(this DateTime time) { DateTime dateTimeStart = TimeZoneInfo.ConvertTimeToUtc(new DateTime(1970, 1, 1)); long t = (time.Ticks - dateTimeStart.Ticks) / 10000;//除10000调整为13位 return t; } } }