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 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;
}
public static string TomAge(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}岁{month}个月";
}
///
/// 岁数
///
/// 月龄
///
public static string TomAge(this int month)
{
int age = month / 12;
int m = month % 12;
return m <= 0 ? $"{age}岁" : $"{age}岁{m}个月";
}
///
/// 相差的月份
///
///
///
public static int ToMonth(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*12 : age*12+month;
}
///
/// 获取时间戳
///
///
///
public static int GetTimeStamp(this DateTime time)
{
int t = Convert.ToInt32((time.ToUniversalTime().Ticks - 621355968000000000) / 10000000);
return t;
}
}
}