DateTimeUtility.cs 911 B

12345678910111213141516171819202122232425262728
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace HouXing.BBS.Entity
  7. {
  8. public static class DateTimeUtility
  9. {
  10. // 时间戳转为C#格式时间
  11. public static DateTime StampToDateTime(this string timeStamp)
  12. {
  13. DateTime dateTimeStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
  14. long lTime = long.Parse(timeStamp + "0000000");
  15. TimeSpan toNow = new TimeSpan(lTime);
  16. return dateTimeStart.Add(toNow);
  17. }
  18. // DateTime时间格式转换为Unix时间戳格式
  19. public static int DateTimeToStamp(this System.DateTime time)
  20. {
  21. System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
  22. return (int)(time - startTime).TotalSeconds;
  23. }
  24. }
  25. }