dateUtil.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /**
  2. * Independent time operation tool to facilitate subsequent switch to dayjs
  3. */
  4. // TODO 芋艿:【锁屏】可能后面删除掉
  5. import dayjs from 'dayjs'
  6. const DATE_TIME_FORMAT = 'YYYY-MM-DD HH:mm:ss'
  7. const DATE_FORMAT = 'YYYY-MM-DD'
  8. const DATE_FORMAT2 = 'YYYY-MM'
  9. export function formatToDateTime(date?: dayjs.ConfigType, format = DATE_TIME_FORMAT): string {
  10. return dayjs(date).format(format)
  11. }
  12. export function formatToDate(date?: dayjs.ConfigType, format = DATE_FORMAT): string {
  13. return dayjs(date).format(format)
  14. }
  15. export function formatToDate2(date?: dayjs.ConfigType, format = DATE_FORMAT2): string {
  16. return dayjs(date).format(format)
  17. }
  18. export function getMonthStartAndEndSimplified(date = new Date()) {
  19. const startDate = new Date(date)
  20. startDate.setDate(1)
  21. const year = startDate.getFullYear()
  22. const month = startDate.getMonth() + 1
  23. const formattedStartDate = startDate.toISOString().split('T')[0]
  24. const endDate = new Date(year, month, 0) // 直接利用月份超出范围的特性
  25. endDate.setDate(endDate.getDate()) // 这行代码其实可以省略,因为endDate已经是最后一天了
  26. const formattedEndDate = endDate.toISOString().split('T')[0]
  27. return [formattedStartDate, formattedEndDate]
  28. }
  29. export function getWeekStartAndEnd(date = new Date()) {
  30. // 复制传入的日期对象
  31. const startDate = new Date(date)
  32. // 获取当前日期是周几(0代表周日,1代表周一,...,6代表周六)
  33. const dayOfWeek = startDate.getDay()
  34. // 计算本周一的日期
  35. // 如果今天是周一(dayOfWeek === 1),则不需要调整
  36. // 如果不是,则需要向前移动到周一
  37. startDate.setDate(startDate.getDate() - dayOfWeek + 1)
  38. // 创建结束日期(周日)
  39. // 本周日就是本周一加上6天
  40. const endDate = new Date(startDate)
  41. endDate.setDate(endDate.getDate() + 6)
  42. // 格式化日期为 YYYY-MM-DD
  43. function formatDate(d: any) {
  44. const year = d.getFullYear()
  45. const month = String(d.getMonth() + 1).padStart(2, '0') // 月份从0开始,所以+1
  46. const day = String(d.getDate()).padStart(2, '0')
  47. return `${year}-${month}-${day}`
  48. }
  49. // 返回格式化后的开始日期和结束日期
  50. return [formatDate(startDate), formatDate(endDate)]
  51. }
  52. export function getYearStartAndEnd(date = new Date()) {
  53. // 获取当前年份
  54. const year = date.getFullYear()
  55. // 创建本年的开始日期(1月1日)
  56. const startDate = new Date(year, 0, 1) // 月份是从0开始的,所以0代表1月
  57. // 创建本年的结束日期(12月31日)
  58. const endDate = new Date(year, 11, 31) // 11代表12月
  59. // 格式化日期为 YYYY-MM-DD
  60. function formatDate(d) {
  61. const year = d.getFullYear()
  62. const month = String(d.getMonth() + 1).padStart(2, '0') // 月份从0开始,所以+1
  63. const day = String(d.getDate()).padStart(2, '0')
  64. return `${year}-${month}-${day}`
  65. }
  66. // 返回格式化后的开始日期和结束日期
  67. return [formatDate(startDate), formatDate(endDate)]
  68. }
  69. /**
  70. * 格式化日期为YYYY-MM-DD格式
  71. * @param {Date} date - 要格式化的日期对象,默认为当前日期
  72. * @returns {string} 格式化后的日期字符串
  73. */
  74. export function formatDateYYMMDD (date = new Date()){
  75. const year = date.getFullYear();
  76. // 月份从0开始,需要+1并补零
  77. const month = String(date.getMonth() + 1).padStart(2, '0');
  78. // 日期补零
  79. const day = String(date.getDate()).padStart(2, '0');
  80. return `${year}-${month}-${day}`;
  81. }
  82. /**
  83. * 比较两个时间字符串的大小
  84. * @param {string} time1 - 第一个时间字符串,格式为HH:mm
  85. * @param {string} time2 - 第二个时间字符串,格式为HH:mm
  86. * @returns {number} - 如果time1 > time2返回1,如果time1 < time2返回-1,如果相等返回0
  87. */
  88. export function compareTimeStrings(time1, time2) {
  89. // 解析时间字符串为分钟数
  90. const convertToMinutes = (timeStr) => {
  91. const [hours, minutes] = timeStr.split(':').map(Number);
  92. return hours * 60 + minutes;
  93. };
  94. const minutes1 = convertToMinutes(time1);
  95. const minutes2 = convertToMinutes(time2);
  96. // 比较大小
  97. if (minutes1 > minutes2) {
  98. return 1;
  99. } else if (minutes1 < minutes2) {
  100. return -1;
  101. } else {
  102. return 0;
  103. }
  104. }
  105. // 检查时间格式的辅助函数
  106. export function isValidTimeFormat(timeStr) {
  107. const regex = /^([01]?[0-9]|2[0-3]):[0-5][0-9]$/;
  108. return regex.test(timeStr);
  109. }
  110. const chineseNumbers = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
  111. //const chineseUnits = ['', '十', '百', '千'];
  112. //const chineseBigUnits = ['', '万', '亿'];
  113. /**
  114. * 数字转中文工具类
  115. */
  116. export function NumberToChineseUtils (num:number){
  117. // 中文数字映射表
  118. if (num < 1 || num > 20) {
  119. throw new Error('目前仅支持1-20的数字转换');
  120. }
  121. if (num < 10) {
  122. return chineseNumbers[num];
  123. }
  124. if (num === 10) {
  125. return '十';
  126. }
  127. if (num < 20) {
  128. return '十' + chineseNumbers[num % 10];
  129. }
  130. if (num === 20) {
  131. return '二十';
  132. }
  133. return '';
  134. }
  135. /**
  136. * 获取当前月份的第一天和最后一天
  137. * @param date - 日期对象,默认为当前日期
  138. * @returns 包含第一天和最后一天的字符串数组,格式为 [YYYY-MM-DD, YYYY-MM-DD]
  139. */
  140. export function getCurrentMonthRange(date = new Date()) {
  141. const year = date.getFullYear();
  142. const month = date.getMonth(); // 0-11
  143. // 本月第一天
  144. const firstDay = new Date(year, month, 1);
  145. // 本月最后一天
  146. const lastDay = new Date(year, month + 1, 0);
  147. // 格式化为 YYYY-MM-DD 字符串
  148. const formatToYYYYMMDD = (d: Date) => {
  149. const year = d.getFullYear();
  150. const month = String(d.getMonth() + 1).padStart(2, '0');
  151. const day = String(d.getDate()).padStart(2, '0');
  152. return `${year}-${month}-${day}`;
  153. };
  154. return [formatToYYYYMMDD(firstDay), formatToYYYYMMDD(lastDay)];
  155. }
  156. /**
  157. * 获取当前月份的第一天和最后一天
  158. * @param date - 日期对象,默认为当前日期
  159. * @returns 包含第一天和最后一天的字符串数组,格式为 [YYYY-MM-DD, YYYY-MM-DD]
  160. */
  161. export function getCurrentMonthRange_1(date = new Date()) {
  162. const year = date.getFullYear();
  163. const month = date.getMonth(); // 0-11
  164. // 本月第一天
  165. const firstDay = new Date(year, month, 1);
  166. // 本月最后一天
  167. const lastDay = new Date(year, month + 1, 1);
  168. // 格式化为 YYYY-MM-DD 字符串
  169. const formatToYYYYMM = (d: Date) => {
  170. const year = d.getFullYear();
  171. const month = String(d.getMonth() + 1).padStart(2, '0');
  172. return `${year}-${month}`;
  173. };
  174. return [formatToYYYYMM(firstDay), formatToYYYYMM(lastDay)];
  175. }
  176. /**
  177. * 将 13 位时间戳转换为指定格式的日期字符串
  178. * @param timestamp - 13 位时间戳
  179. * @param format - 日期格式,默认为 'YYYY-MM-DD HH:mm:ss'
  180. * @returns 格式化后的日期字符串
  181. */
  182. export function formatTimestamp(timestamp: number, format = DATE_TIME_FORMAT): string {
  183. // 确保时间戳是 13 位,如果不是则转换
  184. const timestamp13 = timestamp.toString().length === 13 ? timestamp : timestamp * 1000;
  185. return dayjs(timestamp13).format(format);
  186. }
  187. /**
  188. * 将 13 位时间戳转换为指定格式的日期字符串
  189. * @param timestamp - 13 位时间戳
  190. * @param format - 日期格式,默认为 'YYYY-MM-DD HH:mm:ss'
  191. * @returns 格式化后的日期字符串
  192. */
  193. export function formatTimestampYMD(timestamp: number, format = DATE_FORMAT): string {
  194. // 确保时间戳是 13 位,如果不是则转换
  195. const timestamp13 = timestamp.toString().length === 13 ? timestamp : timestamp * 1000;
  196. return dayjs(timestamp13).format(format);
  197. }
  198. export const dateUtil = dayjs