TimeUtil.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class TimeUtil
  5. {
  6. //格式化,最终得出分和秒
  7. public static string GetTimeStr(float time, bool ceil = true)
  8. {
  9. int seconds = ceil ? Mathf.CeilToInt(time) : Mathf.FloorToInt(time);
  10. string str = "";
  11. int m = seconds / 60;
  12. if (m < 10) {
  13. str += 0;
  14. }
  15. str += m;
  16. str += " : ";
  17. int s = seconds % 60;
  18. if (s < 10)
  19. {
  20. str += 0;
  21. }
  22. str += s;
  23. return str;
  24. }
  25. public static string GetOfflineTimeStr(long offlineTime, bool online) {
  26. if (online) return "在线";
  27. long now = JC.CS.Utility.GetTimestamp();
  28. long dt = now - offlineTime;
  29. if (dt < 0) dt = 0;
  30. if (offlineTime == 0) return "离线";
  31. long min = dt / (60 * 1000);
  32. if (min == 0) min += 1;
  33. if (min < 60) {
  34. return min + "分钟前";
  35. }
  36. long hour = min / 60;
  37. if (hour < 24) {
  38. return hour + "小时前";
  39. }
  40. long day = hour / 24;
  41. if (day < 365) {
  42. return day + "天前";
  43. }
  44. long year = day / 365;
  45. return year + "年前";
  46. }
  47. }