TimeUtils.cs 1.0 KB

12345678910111213141516171819202122232425262728293031
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class TimeUtils
  6. {
  7. /// <summary>
  8. /// 将 DateTime时间格式转换为Unix时间戳格式
  9. /// </summary>
  10. /// <param name="time">时间</param>
  11. /// <returns>long</returns>
  12. public static long ConvertToTimeStamp(DateTime time)
  13. {
  14. System.DateTime Time = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
  15. long TimeStamp = (time.Ticks - Time.Ticks) / 10000; //除10000调整为13位
  16. return TimeStamp;
  17. }
  18. /// <summary>
  19. /// 将Unix时间戳格式 转换为DateTime时间格式
  20. /// </summary>
  21. /// <param name="time">时间</param>
  22. /// <returns>long</returns>
  23. public static DateTime ConvertToDateTime(long time)
  24. {
  25. System.DateTime Time = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
  26. DateTime dateTime = Time.AddMilliseconds(time);
  27. return dateTime;
  28. }
  29. }