LongExtension.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. namespace Alchemy.Core.Extensions
  2. {
  3. public static class LongExtension
  4. {
  5. /// <summary>
  6. /// 时间戳转DateTime
  7. /// </summary>
  8. /// <param name="timeStamp"></param>
  9. /// <returns></returns>
  10. public static DateTime ToDateTime(this long timeStamp)
  11. {
  12. DateTime dt = new DateTime(2000, 1, 1, 0, 0, 0);
  13. try
  14. {
  15. dt = DateTimeOffset.FromUnixTimeSeconds(timeStamp).LocalDateTime;
  16. }
  17. catch (Exception ex)
  18. {
  19. throw new Exception(ex.Message);
  20. }
  21. return dt;
  22. }
  23. /// <summary>
  24. /// 时间戳转DateTimeUtc
  25. /// </summary>
  26. /// <param name="timeStamp"></param>
  27. /// <returns></returns>
  28. public static DateTime ToDateTimeUtc(this long timeStamp)
  29. {
  30. DateTime dt = new DateTime(2000, 1, 1, 0, 0, 0);
  31. try
  32. {
  33. dt = DateTimeOffset.FromUnixTimeSeconds(timeStamp).DateTime;
  34. }
  35. catch (Exception ex)
  36. {
  37. throw new Exception(ex.Message);
  38. }
  39. return dt;
  40. }
  41. }
  42. }