/*
|
* @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;
|