DateUtils.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System;
  2. using System.Collections;
  3. using UnityEngine;
  4. namespace GFGGame
  5. {
  6. public class DateUtils : SingletonBase<DateUtils>
  7. {
  8. public const int TIME_FORMAT_1 = 1;
  9. public int MONTH_PER_YEAR = 12;//每年的月数
  10. public int DAYS_PER_MONTH = 30;//每月的天数
  11. public int HOURS_PER_DAY = 24;//每天的小时数
  12. public int MUNITE_PER_HOUR = 60;//每小时的分钟数
  13. public int SECOND_PER_MUNITE = 60;//每分钟的秒数
  14. public int SECOND_PER_DAY = 86400;//一天的秒数
  15. public int GetCurTime()
  16. {
  17. var utcNow = DateTime.UtcNow;
  18. var timeSpan = utcNow - new DateTime(1970, 1, 1, 0, 0, 0);
  19. return (int)timeSpan.TotalSeconds;
  20. }
  21. public string getFormatBySecond(int second, int type = 1, int showLength = 2)
  22. {
  23. string str = "";
  24. int ms = second * 1000;
  25. switch (type)
  26. {
  27. case DateUtils.TIME_FORMAT_1:
  28. str = this.format_1(second);
  29. break;
  30. }
  31. return str;
  32. }
  33. //剩余时间大于1天返回天数,小于一天返回小时数,小于一小时返回分钟数,小于1分钟返回秒数
  34. public string format_1(int second)
  35. {
  36. if (second / this.SECOND_PER_DAY >= 1)
  37. {
  38. return string.Format ("剩余{0}天", Mathf.Floor(second / this.SECOND_PER_DAY));
  39. }
  40. else if(second / this.SECOND_PER_DAY<1&& second/(this.MUNITE_PER_HOUR*this.SECOND_PER_MUNITE) >= 1)
  41. {
  42. return string.Format("剩余{0}小时", Mathf.Floor(second /( this.MUNITE_PER_HOUR * this.SECOND_PER_MUNITE)));
  43. }
  44. else if (second / this.SECOND_PER_DAY < 1 && second / (this.MUNITE_PER_HOUR * this.SECOND_PER_MUNITE) < 1&& second/this. SECOND_PER_MUNITE>=1)
  45. {
  46. return string.Format("剩余{0}分钟", Mathf.Floor(second / this.SECOND_PER_MUNITE));
  47. }else if (second< this.SECOND_PER_MUNITE)
  48. {
  49. return string.Format("剩余{0}秒", second);
  50. }
  51. return "";
  52. }
  53. }
  54. }