123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace IoTIntegrationPlatform.Common
- {
- /// <summary>
- /// 时间公共类
- /// </summary>
- public static class TimeHelper
- {
- /// <summary>
- /// 相差秒
- /// </summary>
- /// <param name="startTime"></param>
- /// <param name="endTime"></param>
- /// <returns></returns>
- public static double DiffSeconds(DateTime startTime, DateTime endTime)
- {
- TimeSpan secondSpan = new TimeSpan(endTime.Ticks - startTime.Ticks);
- return secondSpan.TotalSeconds;
- }
- /// <summary>
- /// 相差分钟数
- /// </summary>
- /// <param name="startTime"></param>
- /// <param name="endTime"></param>
- /// <returns></returns>
- public static double DiffMinutes(DateTime startTime, DateTime endTime)
- {
- TimeSpan minuteSpan = new TimeSpan(endTime.Ticks - startTime.Ticks);
- return minuteSpan.TotalMinutes;
- }
- /// <summary>
- /// 相差小时
- /// </summary>
- /// <param name="startTime"></param>
- /// <param name="endTime"></param>
- /// <returns></returns>
- public static double DiffHours(DateTime startTime, DateTime endTime)
- {
- TimeSpan hoursSpan = new TimeSpan(endTime.Ticks - startTime.Ticks);
- return hoursSpan.TotalHours;
- }
- /// <summary>
- /// 相差天数
- /// </summary>
- /// <param name="startTime"></param>
- /// <param name="endTime"></param>
- /// <returns></returns>
- public static double DiffDays(DateTime startTime, DateTime endTime)
- {
- TimeSpan daysSpan = new TimeSpan(endTime.Ticks - startTime.Ticks);
- return daysSpan.TotalDays;
- }
- /// <summary>
- /// 相差月数
- /// </summary>
- /// <param name="startTime"></param>
- /// <param name="endTime"></param>
- /// <returns></returns>
- public static double DiffMonths(DateTime startTime, DateTime endTime)
- {
- int months = (endTime.Year - startTime.Year) * 12 + endTime.Month - startTime.Month;
- if (startTime.Day > endTime.Day) months--; // 如果第一个日期的日大于第二个日期的日,则减去一个月
- return months;
- }
- /// <summary>
- /// 获取当前时间戳
- /// </summary>
- /// <returns></returns>
- public static long GetCurrentTimestamp()
- {
- DateTimeOffset currentTime = DateTimeOffset.Now;
- long timestamp = currentTime.ToUnixTimeSeconds();
- return timestamp;
- }
- /// <summary>
- /// 获取当前时间戳[毫秒]
- /// </summary>
- /// <param name="dateTime"></param>
- /// <returns></returns>
- public static long GetCurrentTimestampTicks()
- {
- TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
- long t = Convert.ToInt64(ts.TotalMilliseconds);
- return t;
- }
- /// <summary>
- /// 时间转时间戳[毫秒]
- /// </summary>
- /// <param name="date"></param>
- /// <returns></returns>
- public static long DateToTimestampTicks(DateTime date)
- {
- DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));//当地时区
- TimeSpan ts = date - startTime;
- long timestamp = Convert.ToInt64(ts.TotalSeconds);
- return timestamp;
- }
- /// <summary>
- /// 某天的开始
- /// </summary>
- /// <param name="datetime"></param>
- /// <returns></returns>
- public static DateTime DayOfZeroSeconds(DateTime datetime)
- {
- return datetime.AddDays(1 - datetime.Day).Date;
- }
- /// <summary>
- /// 某天的最后一秒
- /// </summary>
- /// <param name="datetime"></param>
- /// <returns></returns>
- public static DateTime DayOfLastSeconds(DateTime datetime)
- {
- return datetime.AddDays(1).AddSeconds(-1);
- }
- }
- }
|