YangYuGang
2025-03-08 8cae1dcd8d2bde01880ac4b70bdda4e61df3c7ef
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
 * @Description: file content
 * @Author: Ben Lin
 * @version: 
 * @Date: 2024-06-18 15:09:47
 * @LastEditors: Ben Lin
 * @LastEditTime: 2024-07-04 20:04:14
 */
/**
 * Independent time operation tool to facilitate subsequent switch to dayjs
 */
import dayjs from 'dayjs';
 
const DATE_TIME_FORMAT = 'YYYY-MM-DD HH:mm:ss';
const DATE_FORMAT = 'YYYY-MM-DD';
 
export function formatToDateTime(date?: dayjs.ConfigType, format = DATE_TIME_FORMAT): string {
  return dayjs(date).format(format);
}
 
export function formatToDate(date?: dayjs.ConfigType, format = DATE_FORMAT): string {
  return dayjs(date).format(format);
}
 
export function intToTime(intValue: number): string {
  const hours = Math.floor(intValue / 100);
  const minutes = intValue % 100;
  return `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}`;
}
 
export function tsToHHmm(ts) {
  var date = new Date(ts);
  var hours = date.getHours().toString().padStart(2, '0');
  var minutes = date.getMinutes().toString().padStart(2, '0');
  return hours + minutes;
}
 
export function formatTime(date: Date) {
  const hours = date.getHours().toString().padStart(2, '0');
  const minutes = date.getMinutes().toString().padStart(2, '0');
  return `${hours}:${minutes}`;
}
 
export function convertTimeToDate(time: string) {
  // 假设时间是当天的时间,所以我们需要构造一个包含今天日期和提供的时间的字符串
  const today = new Date();
  const [hours, minutes] = time.split(':').map(Number);
  // 设置时间,保留今天的日期并设置指定的小时和分钟
  today.setHours(hours, minutes, 0, 0);
  return dayjs(today);
}
 
export const dateUtil = dayjs;