445 lines
16 KiB
C#
445 lines
16 KiB
C#
using System;
|
|
using System.Text;
|
|
|
|
namespace Nirvana.Common
|
|
{
|
|
public static partial class Ext
|
|
{
|
|
/// <summary>
|
|
/// 获取格式化字符串,带时分秒,格式:"yyyy-MM-dd HH:mm:ss"
|
|
/// </summary>
|
|
/// <param name="dateTime">日期</param>
|
|
/// <param name="isRemoveSecond">是否移除秒</param>
|
|
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");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取格式化字符串,带时分秒,格式:"yyyy-MM-dd HH:mm:ss"
|
|
/// </summary>
|
|
/// <param name="dateTime">日期</param>
|
|
/// <param name="isRemoveSecond">是否移除秒</param>
|
|
public static string ToDateTimeString(this DateTime? dateTime, bool isRemoveSecond = false)
|
|
{
|
|
if (dateTime == null)
|
|
return string.Empty;
|
|
return ToDateTimeString(dateTime.Value, isRemoveSecond);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取格式化字符串,不带时分秒,格式:"yyyy-MM-dd"
|
|
/// </summary>
|
|
/// <param name="dateTime">日期</param>
|
|
public static string ToDateString(this DateTime dateTime)
|
|
{
|
|
return dateTime.ToString("yyyy-MM-dd");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取格式化字符串,不带时分秒,格式:"yyyy-MM-dd"
|
|
/// </summary>
|
|
/// <param name="dateTime">日期</param>
|
|
public static string ToDateString()
|
|
{
|
|
return DateTime.Now.ToString("yyyy-MM-dd");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取格式化字符串,不带时分秒,格式:"yyyy-MM-dd"
|
|
/// </summary>
|
|
/// <param name="dateTime">日期</param>
|
|
public static string ToDateString(this DateTime? dateTime)
|
|
{
|
|
if (dateTime == null)
|
|
return string.Empty;
|
|
return ToDateString(dateTime.Value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取格式化字符串,不带年月日,格式:"HH:mm:ss"
|
|
/// </summary>
|
|
/// <param name="dateTime">日期</param>
|
|
public static string ToTimeString(this DateTime dateTime)
|
|
{
|
|
return dateTime.ToString("HH:mm:ss");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取格式化字符串,不带年月日,格式:"HH:mm:ss"
|
|
/// </summary>
|
|
/// <param name="dateTime">日期</param>
|
|
public static string ToTimeString(this DateTime? dateTime)
|
|
{
|
|
if (dateTime == null)
|
|
return string.Empty;
|
|
return ToTimeString(dateTime.Value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取格式化字符串,带毫秒,格式:"yyyy-MM-dd HH:mm:ss.fff"
|
|
/// </summary>
|
|
/// <param name="dateTime">日期</param>
|
|
public static string ToMillisecondString(this DateTime dateTime)
|
|
{
|
|
return dateTime.ToString("yyyy-MM-dd HH:mm:ss.fff");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取格式化字符串,带毫秒,格式:"yyyy-MM-dd HH:mm:ss.fff"
|
|
/// </summary>
|
|
/// <param name="dateTime">日期</param>
|
|
public static string ToMillisecondString(this DateTime? dateTime)
|
|
{
|
|
if (dateTime == null)
|
|
return string.Empty;
|
|
return ToMillisecondString(dateTime.Value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取格式化字符串,不带时分秒,格式:"yyyy年MM月dd日"
|
|
/// </summary>
|
|
/// <param name="dateTime">日期</param>
|
|
public static string ToChineseDateString(this DateTime dateTime)
|
|
{
|
|
return string.Format("{0}年{1}月{2}日", dateTime.Year, dateTime.Month, dateTime.Day);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取格式化字符串,不带时分秒,格式:"yyyy年MM月dd日"
|
|
/// </summary>
|
|
/// <param name="dateTime">日期</param>
|
|
public static string ToChineseDateString(this DateTime? dateTime)
|
|
{
|
|
if (dateTime == null)
|
|
return string.Empty;
|
|
return ToChineseDateString(dateTime.SafeValue());
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取格式化字符串,带时分秒,格式:"yyyy年MM月dd日 HH时mm分"
|
|
/// </summary>
|
|
/// <param name="dateTime">日期</param>
|
|
/// <param name="isRemoveSecond">是否移除秒</param>
|
|
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();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取格式化字符串,带时分秒,格式:"yyyy年MM月dd日 HH时mm分"
|
|
/// </summary>
|
|
/// <param name="dateTime">日期</param>
|
|
/// <param name="isRemoveSecond">是否移除秒</param>
|
|
public static string ToChineseDateTimeString(this DateTime? dateTime, bool isRemoveSecond = false)
|
|
{
|
|
if (dateTime == null)
|
|
return string.Empty;
|
|
return ToChineseDateTimeString(dateTime.Value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 时间戳转时间
|
|
/// </summary>
|
|
/// <param name="timeStamp"></param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 时间戳转时间
|
|
/// </summary>
|
|
/// <param name="timeStamp"></param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 时间转时间戳
|
|
/// </summary>
|
|
/// <param name="time"></param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 出生年月转为年龄
|
|
/// </summary>
|
|
/// <param name="birthdate"></param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 岁数,小于16岁的显示几岁几个月,其他只显示整岁
|
|
/// </summary>
|
|
/// <param name="birthdate"></param>
|
|
/// <returns></returns>
|
|
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}个月");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 岁数
|
|
/// </summary>
|
|
/// <param name="birthdate"></param>
|
|
/// <returns></returns>
|
|
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}个月";
|
|
}
|
|
|
|
/// <summary>
|
|
/// 岁数
|
|
/// </summary>
|
|
/// <param name="month">月龄</param>
|
|
/// <returns></returns>
|
|
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}个月";
|
|
}
|
|
|
|
/// <summary>
|
|
/// 相差的天数
|
|
/// </summary>
|
|
/// <param name="birthdate"></param>
|
|
/// <returns></returns>
|
|
public static int ToBirthDay(this DateTime birthdate)
|
|
{
|
|
DateTime now = DateTime.Now;
|
|
TimeSpan sp = now.Subtract(birthdate);
|
|
return sp.Days;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 相差的月份,返回带小数点的
|
|
/// </summary>
|
|
/// <param name="birthdate"></param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 相差的月份
|
|
/// </summary>
|
|
/// <param name="birthdate"></param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 相差的月份
|
|
/// </summary>
|
|
/// <param name="birthdate">出生年月</param>
|
|
/// <param name="IsWeek">是否精确到周,false-否</param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 相差的月份
|
|
/// </summary>
|
|
/// <param name="birthdate"></param>
|
|
/// <param name="starttime">开始时间</param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
}
|
|
} |