CUtilTime.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * 时间工具
  3. */
  4. export default class CUtilTime {
  5. public static getNowTime() {
  6. let d = new Date();
  7. return Math.floor(d.getTime() / 1000);
  8. }
  9. public static getNowDateInt() {
  10. let d = new Date();
  11. let curr_date = '' + d.getDate();
  12. let curr_month = '' + (d.getMonth() + 1);
  13. let curr_year = '' + d.getFullYear();
  14. if (curr_month.length < 2) {
  15. curr_month = '0' + curr_month;
  16. }
  17. if (curr_date.length < 2) {
  18. curr_date = '0' + curr_date;
  19. }
  20. var yyyyMMdd = curr_year + curr_month + curr_date;
  21. return parseInt(yyyyMMdd);
  22. }
  23. /*获取一个月的天数 */
  24. public static getCountDays() {
  25. let curDate = new Date();
  26. /* 获取当前月份 */
  27. let curMonth = curDate.getMonth();
  28. /*  生成实际的月份: 由于curMonth会比实际月份小1, 故需加1 */
  29. curDate.setMonth(curMonth + 1);
  30. /* 将日期设置为0, 这里为什么要这样设置, 我不知道原因, 这是从网上学来的 */
  31. curDate.setDate(0);
  32. /* 返回当月的天数 */
  33. return curDate.getDate();
  34. }
  35. public static getTimeString(syTime: number): string {
  36. let hours = Math.floor(syTime / 60 / 60); //计算剩余的小时
  37. let minutes = Math.floor(syTime / 60 % 60);
  38. let seconds = Math.floor(syTime % 60);
  39. let day = Math.floor(hours / 24);
  40. let hour = hours - day * 24;
  41. if (day > 0) {
  42. return day + '天' + hour + '小时' + minutes + '分钟' + seconds + '秒';
  43. }
  44. if (hour > 0) {
  45. return hour + '小时' + minutes + '分钟' + seconds + '秒';
  46. }
  47. if (minutes > 0) {
  48. return minutes + '分钟' + seconds + '秒';
  49. }
  50. return seconds + '秒';
  51. }
  52. public static getTimeString2(syTime: number) {
  53. let hours = Math.floor(syTime / 60 / 60); //计算剩余的小时
  54. let minutes = Math.floor(syTime / 60 % 60);
  55. let seconds = Math.floor(syTime % 60);
  56. let min = minutes > 0 && minutes < 10 ? "0" + minutes : minutes == 0 ? "00" : minutes;
  57. let sec = seconds > 0 && seconds < 10 ? "0" + seconds : seconds == 0 ? "00" : seconds;
  58. if (hours > 0) {
  59. return hours + ':' + min + ':' + sec;
  60. }
  61. if (minutes > 0) {
  62. return min + ':' + sec;
  63. }
  64. return sec;
  65. }
  66. public static _getTimeString(syTime: number): string {
  67. let hours = Math.floor(syTime / 60 / 60); //计算剩余的小时
  68. let minutes = Math.floor(syTime / 60 % 60);
  69. let seconds = Math.floor(syTime % 60);
  70. let day = Math.floor(hours / 24);
  71. let hour = hours - day * 24;
  72. if (day > 0) {
  73. return day + '天' + hour + '小时';
  74. }
  75. if (hour > 0) {
  76. return hour + '小时' + minutes + '分钟';
  77. }
  78. if (minutes > 0) {
  79. return minutes + '分' + seconds + '秒';
  80. }
  81. return seconds + '秒';
  82. }
  83. public static _getTimeOnlineString(syTime: number): string {
  84. let hours = Math.floor(syTime / 60 / 60); //计算剩余的小时
  85. let minutes = Math.floor(syTime / 60 % 60);
  86. let seconds = Math.floor(syTime % 60);
  87. let day = Math.floor(hours / 24);
  88. let hour = hours - day * 24;
  89. if (day > 0) {
  90. return day + '天';
  91. }
  92. if (hour > 0) {
  93. return hour + '小时';
  94. }
  95. if (minutes > 0) {
  96. return minutes + '分钟';
  97. }
  98. return seconds + '秒';
  99. }
  100. }