DateTime.Now 这里要特别说明下,访问此属性会调取当前服务器设置的时间,与用户系统时间无关
new DateTime 官方重载了很多方法,这里只介绍了最常用的方法
new DateTime(2022,8,31)
DateTime today = DateTime.Now;
DateTime todayZeroPoint = new DateTime(today.Year, today.Month, today.Day+1)
不要按照上述的例子去写,当日期恰好是31或本月无31天时,会报年、月和日参数描述无法表示的 DateTime。
参数1 设置日期的格式
参数2 传入的参数
string.Format("{0:yyyy-MM-dd HH:mm:ss}", DateTime.Now)
string.Format("{0:yyyy-MM-dd}", DateTime.Now)
$"{DateTime.Now:yyyy-MM-dd}"
new DateTime(2000, 1, 1).ToString("yyyy-MM-dd HH:mm:ss") ToString 可能会被重写,使用时需注意
Convert.ToDateTime("2022-08-31")
DateTime.Parse("2020-08-31 13:00:00")
利用正则将数字转换时间
Convert.ToDateTime("20211022".Replace("(\d{4})\d{2}(\d{2})","$1-$2-$3"));
DateTime.MaxValue : 9999/12/31 23:59:59`
DateTime.MinValue : 0001/1/1 0:00:00`
DateTime 未赋值,访问会自动设置 DateTime.MinValue`
DateTime? 会自动默认 null
DateTime.Now.Date
DateTime.Now.ToLongDateString()
值对应1~7
DateTime.Now.DayOfWeek
可设置指定日期零点
DateTime today = DateTime.Now;
DateTime todayZeroPoint = new DateTime(today.Year, today.Month, today.Day);
今天零点
DateTime.Now.Date
1年前
DateTime.Now.AddYears(-1)
1月前
DateTime.Now.AddMonths(-1)
i天前
DateTime.Now.AddDays(-1)
1小时前
DateTime.Now.AddHours(-1)
1分钟前
DateTime.Now.AddMinutes(-1)
1秒前
DateTime.Now.AddSeconds(-1)
1毫秒
DateTime.Now.AddMilliseconds(-1)
需要判断下天数,不然小时永远小于24小时
var withinProtectionTime=DateTime.Now.Subtract(DateTime.Now.AddHours(-5));
withinProtectionTime.Days == 0&&withinProtectionTime.Hours <= 24
过去
DateTime.Now.Subtract(DateTime.Now.AddHours(24)).TotalSeconds<0
未来
DateTime.Now.Subtract(DateTime.Now.AddHours(-24)).TotalSeconds > 0
var datestr = DateTime.Now.Subtract(DateTime.Parse(addtime).AddHours(48));
datestrTip= datestr.Days.ToString() + "天" + datestr.Hours.ToString() + "小时" + datestr.Minutes.ToString() + "分钟" + datestr.Seconds.ToString() + "秒";
fcDate.TotalSeconds<0;
(endTime- startTime).TotalMilliseconds.ToString(CultureInfo.InvariantCulture)
TimeSpan.FromMinutes(200)
直接转换成字符串,在去掉秒,不要在ToString 后面直接写时间格式,因当前结构是TimeSpan
var timeTotalCount = TimeSpan.FromMinutes(200).ToString();
timeTotalCount = timeTotalCount.StartsWith("00:") ? timeTotalCount.Substring(3) : timeTotalCount;
转换成时间,在利用ToString转换成字符串
new DateTime(1970, 1, 1).Add(TimeSpan.FromMinutes(200)).ToString("HH:mm")
R: 为什么时间戳中会出现 1970-01-01 00:00:00.000 这个时间
Q: 1970年1月1日作为时间计算起点,UNIX TIME 的纪元
///
/// 时间类型转换时间戳
///
/// 时间
/// 是否到毫秒
/// 时间戳
public static long ConvertDateTimeToUnixTimestamp(System.DateTime time, bool isms = false)
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
if (isms)
{
return (time.Ticks - startTime.Ticks) / 10000;
}
return (time.Ticks - startTime.Ticks) / 10000000; //除10000调整为13位
}
///
/// 时间戳转换时间
///
/// 时间刻度
///
public static string ConvertStringToDateTime(string timeStamp)
{
if (string.IsNullOrWhiteSpace(timeStamp)) return timeStamp;
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
long lTime = long.Parse(timeStamp + "0000");
TimeSpan toNow = new TimeSpan(lTime);
return dtStart.Add(toNow).ToString("yyyy-MM-dd HH:mm:ss");
}
DateTimeOffset.UtcNow.ToUnixTimeSeconds()
DateTimeOffset.FromUnixTimeSeconds(1661957086)
DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
DateTimeOffset.FromUnixTimeMilliseconds(1661957169231)
.Select(e => e.adddate < Convert.ToDateTime("2020-08-31"))
.where(e.adddate>DateTime.Now.AddDays(-90))
最大时间
new List<DateTime> { DateTime.Now , DateTime.Now.AddDays(-90) }.Max()
最小时间
new List<DateTime> { DateTime.Now , DateTime.Now.AddDays(-90) }.Max()
具体时间可以根据上下班设置
DateTime today = DateTime.Now;
DateTime todayZeroPoint = new DateTime(today.Year, today.Month, today.Day);
if (DateTime.Now> todayZeroPoint && DateTime.Now < new DateTime(today.Year, today.Month, today.Day, 8, 30, 0))
{
return true;
}
DateTime tomorrowZeroPoint = new DateTime(today.Year, today.Month, today.Day).AddDays(1);
if (tomorrowZeroPoint > DateTime.Now && DateTime.Now > new DateTime(today.Year, today.Month, today.Day, 17, 30, 0)) {
return true;
}
///
/// 判断是周六或周天
///
private bool IsSaturdayOrSunday { get { return new List<string>() { "6", "7" }.Contains(DateTime.Now.DayOfWeek.ToString()); } }