dateFormat.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import dayjs from 'dayjs';
  2. import { get } from 'http';
  3. export function getNowDateAndMonday(time: number) {
  4. let timestamp = time;
  5. const serverDate = new Date(time);
  6. if (serverDate.getDay() == 0) {
  7. timestamp -= 7 * 24 * 60 * 60 * 1000;
  8. }
  9. const mondayTime =
  10. timestamp - (serverDate.getDay() - 1) * 24 * 60 * 60 * 1000;
  11. const mondayData = new Date(mondayTime).getTime();
  12. //年
  13. // const mondayY = mondayData.getFullYear();
  14. // //月
  15. // const mondayM =
  16. // mondayData.getMonth() + 1 < 10
  17. // ? '0' + (mondayData.getMonth() + 1)
  18. // : mondayData.getMonth() + 1;
  19. // //日
  20. // const mondayD =
  21. // mondayData.getDate() < 10
  22. // ? '0' + mondayData.getDate()
  23. // : mondayData.getDate();
  24. // const str = mondayY + '-' + mondayM + '-' + mondayD;
  25. return mondayData;
  26. }
  27. export function getNowDateAndSunday(time: number) {
  28. const timestamp = time;
  29. const serverDate = new Date(time);
  30. let num = 7 - serverDate.getDay();
  31. if (num == 7) {
  32. num = 0;
  33. }
  34. const sundayTiem = timestamp + num * 24 * 60 * 60 * 1000;
  35. const SundayData = new Date(sundayTiem).getTime();
  36. //年
  37. // const tomorrowY = SundayData.getFullYear(); //月
  38. // const tomorrowM =
  39. // SundayData.getMonth() + 1 < 10
  40. // ? '0' + (SundayData.getMonth() + 1)
  41. // : SundayData.getMonth() + 1;
  42. // //日
  43. // const tomorrowD =
  44. // SundayData.getDate() < 10
  45. // ? '0' + SundayData.getDate()
  46. // : SundayData.getDate();
  47. // const str = tomorrowY + '-' + tomorrowM + '-' + tomorrowD;
  48. return SundayData;
  49. }
  50. export const getTimes = (
  51. times: any,
  52. keys: Array<string> = [],
  53. format = 'YYYY-MM-DD'
  54. ) => {
  55. if (times && times.length) {
  56. return format == 'YYYY-MM-DD'
  57. ? {
  58. [keys[0] || 'start']: dayjs(times[0]).isValid()
  59. ? dayjs(times[0]).format(format) + ' 00:00:00'
  60. : '',
  61. [keys[1] || 'end']: dayjs(times[1]).isValid()
  62. ? dayjs(times[1]).format(format) + ' 23:59:59'
  63. : ''
  64. }
  65. : {
  66. [keys[0] || 'start']: dayjs(times[0]).isValid()
  67. ? dayjs(times[0]).format(format)
  68. : '',
  69. [keys[1] || 'end']: dayjs(times[1]).isValid()
  70. ? dayjs(times[1]).format(format)
  71. : ''
  72. };
  73. }
  74. return {};
  75. };
  76. export function formatTime(time: number) {
  77. const hours = Math.floor(time / 3600);
  78. const minutes = Math.floor(Math.floor(time % 3600) / 60);
  79. const seconds = Math.floor(time % 60);
  80. const h = hours.toString().length === 1 ? `0${hours}` : hours;
  81. const m = minutes.toString().length === 1 ? `0${minutes}` : minutes;
  82. const s = seconds.toString().length === 1 ? `0${seconds}` : seconds;
  83. let str = '';
  84. if (hours > 0) {
  85. str = `${h}小时${m}分钟${s}秒`;
  86. } else if (minutes > 0) {
  87. str = `${m}分钟${s}秒`;
  88. } else {
  89. str = `${s}秒`;
  90. }
  91. console.log();
  92. return str;
  93. }
  94. export function getHours(time: number) {
  95. const minutes = Math.floor(time / 60 / 60);
  96. return minutes;
  97. }
  98. export function getLastMinutes(time: number) {
  99. const minutes = Math.floor(time / 60);
  100. return Math.floor(minutes % 60)
  101. }
  102. export function getMinutes(time: number) {
  103. const minutes = Math.floor(time / 60);
  104. return minutes;
  105. }
  106. export function getSecend(time: number) {
  107. const seconds = Math.floor(time % 60);
  108. return seconds;
  109. }
  110. export function getChatMinutes(time: number) {
  111. const minutes = Math.floor(time / 60);
  112. let s = 0
  113. if(time % 60) {
  114. s = Math.ceil(time % 60 / 60 * 100) / 100
  115. }
  116. return minutes + s;
  117. }
  118. // 秒转时分秒
  119. export function formateSeconds(endTime: string, pad = 2) {
  120. let secondTime = parseInt(endTime) //将传入的秒的值转化为Number
  121. let min = 0 // 初始化分
  122. let h = 0 // 初始化小时
  123. let result = ''
  124. if (secondTime >= 60) {
  125. //如果秒数大于等于60,将秒数转换成整数
  126. min = parseInt(secondTime / 60 + '') //获取分钟,除以60取整数,得到整数分钟
  127. secondTime = parseInt((secondTime % 60) + '') //获取秒数,秒数取佘,得到整数秒数
  128. if (min >= 60) {
  129. //如果分钟大于等于60,将分钟转换成小时
  130. h = parseInt(min / 60 + '') //获取小时,获取分钟除以60,得到整数小时
  131. min = parseInt((min % 60) + '') //获取小时后取佘的分,获取分钟除以60取佘的分
  132. }
  133. }
  134. if (h) {
  135. result = `${h.toString().padStart(pad, '0')}时${min.toString().padStart(pad, '0')}分${secondTime
  136. .toString()
  137. .padStart(pad, '0')}秒`
  138. } else if (min) {
  139. result = `${min.toString().padStart(pad, '0')}分${secondTime.toString().padStart(pad, '0')}秒`
  140. } else {
  141. result = `${secondTime.toString().padStart(pad, '0')}秒`
  142. }
  143. return result
  144. }