DateUtils.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. //direction排版方向:0横向,1纵向
  22. public string getFormatBySecond(int second, int type = 1, int direction = 0)
  23. {
  24. string str = "";
  25. int ms = second * 1000;
  26. switch (type)
  27. {
  28. case DateUtils.TIME_FORMAT_1:
  29. str = this.format_1(second, direction);
  30. break;
  31. }
  32. return str;
  33. }
  34. //剩余时间大于1天返回天数,小于一天返回小时数,小于一小时返回分钟数,小于1分钟返回秒数
  35. public string format_1(int second, int direction = 0)
  36. {
  37. if (second / this.SECOND_PER_DAY >= 1)
  38. {
  39. if (direction == 0)
  40. {
  41. return string.Format("剩余{0}天", Mathf.Floor(second / this.SECOND_PER_DAY));
  42. }
  43. else
  44. {
  45. return string.Format("剩\n余\n{0}\n天", Mathf.Floor(second / this.SECOND_PER_DAY));
  46. }
  47. }
  48. else if (second / this.SECOND_PER_DAY < 1 && second / (this.MUNITE_PER_HOUR * this.SECOND_PER_MUNITE) >= 1)
  49. {
  50. if (direction == 0)
  51. {
  52. return string.Format("剩余{0}小时", Mathf.Floor(second / (this.MUNITE_PER_HOUR * this.SECOND_PER_MUNITE)));
  53. }
  54. else
  55. {
  56. return string.Format("剩\n余\n{0}\n小\n时", Mathf.Floor(second / (this.MUNITE_PER_HOUR * this.SECOND_PER_MUNITE)));
  57. }
  58. }
  59. else if (second / this.SECOND_PER_DAY < 1 && second / (this.MUNITE_PER_HOUR * this.SECOND_PER_MUNITE) < 1 && second / this.SECOND_PER_MUNITE >= 1)
  60. {
  61. if (direction == 0)
  62. {
  63. return string.Format("剩余{0}分钟", Mathf.Floor(second / this.SECOND_PER_MUNITE));
  64. }
  65. else
  66. {
  67. return string.Format("剩\n余\n{0}\n分\n钟", Mathf.Floor(second / this.SECOND_PER_MUNITE));
  68. }
  69. }
  70. else if (second < this.SECOND_PER_MUNITE)
  71. {
  72. if (direction == 0)
  73. {
  74. return string.Format("剩余{0}秒", second);
  75. }
  76. else
  77. {
  78. return string.Format("剩\n余\n{0}\n秒", second);
  79. }
  80. }
  81. return "";
  82. }
  83. }
  84. }