DateHelper.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace Ropin.Inspection.Common.Helper
  7. {
  8. public class DateHelper
  9. {
  10. public static DateTime GetDateTime(long timestamp)
  11. {
  12. DateTime dtStart = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
  13. long lTime = long.Parse(timestamp + "0000000"); // 如果时间戳是秒级,需要转换为毫秒级
  14. TimeSpan timeSpan = new TimeSpan(lTime);
  15. DateTime targetDt = dtStart.Add(timeSpan);
  16. return targetDt;
  17. }
  18. public static DateTime GetDateTime(long timestamp, TimeZoneInfo targetTimeZone)
  19. {
  20. DateTime dtStart = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
  21. long lTime = long.Parse(timestamp + "0000000"); // 如果时间戳是秒级,需要转换为毫秒级
  22. TimeSpan timeSpan = new TimeSpan(lTime);
  23. DateTime targetDt = dtStart.Add(timeSpan);
  24. return TimeZoneInfo.ConvertTimeFromUtc(targetDt, targetTimeZone);
  25. }
  26. public static DateTime StampToDateTime(string time)
  27. {
  28. time = time.Substring(0, 10);
  29. double timestamp = Convert.ToInt64(time);
  30. System.DateTime dateTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);
  31. dateTime = dateTime.AddSeconds(timestamp).ToLocalTime();
  32. return dateTime;
  33. }
  34. public static string TimeSubTract(DateTime time1, DateTime time2)
  35. {
  36. TimeSpan subTract = time1.Subtract(time2);
  37. return $"{subTract.Days} 天 {subTract.Hours} 时 {subTract.Minutes} 分 ";
  38. }
  39. /// <summary>
  40. /// 时间戳转本地时间-时间戳精确到秒
  41. /// </summary>
  42. public static DateTime ToLocalTimeDateBySeconds(long unix)
  43. {
  44. var dto = DateTimeOffset.FromUnixTimeSeconds(unix);
  45. return dto.ToLocalTime().DateTime;
  46. }
  47. /// <summary>
  48. /// 时间转时间戳Unix-时间戳精确到秒
  49. /// </summary>
  50. public static long ToUnixTimestampBySeconds(DateTime dt)
  51. {
  52. DateTimeOffset dto = new DateTimeOffset(dt);
  53. return dto.ToUnixTimeSeconds();
  54. }
  55. /// <summary>
  56. /// 时间戳转本地时间-时间戳精确到毫秒
  57. /// </summary>
  58. public static DateTime ToLocalTimeDateByMilliseconds(long unix)
  59. {
  60. var dto = DateTimeOffset.FromUnixTimeMilliseconds(unix);
  61. return dto.ToLocalTime().DateTime;
  62. }
  63. /// <summary>
  64. /// 时间转时间戳Unix-时间戳精确到毫秒
  65. /// </summary>
  66. public static long ToUnixTimestampByMilliseconds(DateTime dt)
  67. {
  68. DateTimeOffset dto = new DateTimeOffset(dt);
  69. return dto.ToUnixTimeMilliseconds();
  70. }
  71. /// <summary>
  72. /// 声明期间类型枚举
  73. /// </summary>
  74. public enum Period { Day, Week, Month, Year };
  75. /// <summary>
  76. /// 获取指定期间的起止日期
  77. /// </summary>
  78. /// <param name="period">期间类型</param>
  79. /// <param name="beginDate">开始日期</param>
  80. /// <param name="endDate">结束日期</param>
  81. public static void GetPeriod(Period period, out DateTime beginDate, out DateTime endDate)
  82. {
  83. int year = DateTime.Today.Year;
  84. int month = DateTime.Today.Month;
  85. switch (period)
  86. {
  87. case Period.Year: //年
  88. beginDate = new DateTime(year, 1, 1);
  89. endDate = new DateTime(year, 12, 31);
  90. break;
  91. case Period.Month: //月
  92. beginDate = new DateTime(year, month, 1);
  93. endDate = beginDate.AddMonths(1).AddDays(-1);
  94. break;
  95. case Period.Week: //周
  96. int week = (int)DateTime.Today.DayOfWeek;
  97. if (week == 0) week = 7; //周日
  98. beginDate = DateTime.Today.AddDays(-(week - 1));
  99. endDate = beginDate.AddDays(6);
  100. break;
  101. default: //日
  102. beginDate = DateTime.Today;
  103. endDate = DateTime.Today;
  104. break;
  105. }
  106. }
  107. }
  108. }