You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
4.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace JobCreateFee
{
/// <summary>
/// datetime数据类型拓展
/// </summary>
public static class DateTimeExtension
{
/// <summary>
///将一个时间转换为秒级时间戳
/// </summary>
/// <param name="datetime">时间</param>
/// <returns></returns>
public static long ToTimeStampSeconds(this DateTime datetime)
{
return (long)Math.Round((datetime.ToUniversalTime() - new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero)).TotalSeconds);
}
/// <summary>
///将一个时间转换为毫秒级时间戳
/// </summary>
/// <param name="datetime"></param>
/// <returns></returns>
public static long ToTimeStampMilliSeconds(this DateTime datetime)
{
return (long)Math.Round((datetime.ToUniversalTime() - new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero)).TotalMilliseconds);
}
/// <summary>
/// 获取标准时间戳 秒级
/// </summary>
/// <param name="datetime"></param>
/// <returns></returns>
public static long ToTimeStamp(this DateTime datetime)
{ return ToTimeStampSeconds(datetime); }
/// <summary>
/// 将时间转换为时间字符串编码 yyyyMMddHHmmssffff
/// </summary>
/// <param name="datetime"></param>
/// <returns></returns>
public static string ToTimeStr(this DateTime datetime)
{
return datetime.ToString("yyyyMMddHHmmssffff");
}
/// <summary>
/// 判断时间是否大于1970年的有效时间
/// </summary>
/// <param name="dateTime"></param>
/// <returns></returns>
public static bool IsValidTime(this DateTime dateTime)
{
if (dateTime.Date >= DateTime.Parse("1970-1-1"))
return true;
else return false;
}
/// <summary>
/// 获时间一天中的开始时间
/// </summary>
/// <param name="dateTime"></param>
public static DateTime GetDayStartTime(this DateTime dateTime)
{
return dateTime.Date;
}
/// <summary>
/// 获取时间一天的结束时间
/// </summary>
/// <param name="dateTime"></param>
/// <returns></returns>
public static DateTime GetDateEndTime(this DateTime dateTime)
{
return dateTime.Date.AddDays(1).AddSeconds(-1);
}
/// <summary>
/// 获取时间周的星期一的时间
/// </summary>
/// <param name="dateTime"></param>
/// <returns></returns>
public static DateTime GetWeekStartTime(this DateTime dateTime)
{
return dateTime.GetWeekOfDayTime(1);
}
/// <summary>
/// 获取时间周的周日
/// </summary>
/// <param name="dateTime"></param>
/// <returns></returns>
public static DateTime GetWeekEndTime(this DateTime dateTime)
{
return dateTime.GetWeekOfDayTime(7);
}
/// <summary>
/// 获取时间周的指定周几时间
/// </summary>
/// <param name="dateTime"></param>
/// <param name="Day">周几 1 2 3 4 5 6 7</param>
/// <returns></returns>
public static DateTime GetWeekOfDayTime(this DateTime dateTime, int Day)
{
if (Day < 1)
Day = 1;
if (Day > 7)
Day = (int)dateTime.DayOfWeek;
return dateTime.Subtract(new TimeSpan(dateTime.DayOfWeek - (DayOfWeek)Day, 0, 0, 0)).Date;
}
/// <summary>
/// 获取时间月开始时间
/// </summary>
/// <param name="dateTime"></param>
/// <returns></returns>
public static DateTime GetMonthStartTime(this DateTime dateTime)
{
return new DateTime(dateTime.Year, dateTime.Month, 1).Date;
}
/// <summary>
/// 获取一个月的结束时间
/// </summary>
/// <param name="dateTime"></param>
/// <returns></returns>
public static DateTime GetMonthEndTime(this DateTime dateTime)
{
return dateTime.GetMonthStartTime().AddMonths(1).AddDays(-1);
}
}
}