LongExtension.cs 1.4 KB

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