Files
template-10519/src/utils/time.ts

40 lines
1.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 获取当前时间
*/
export function formatCurrentDate() {
// 创建一个Date对象表示当前日期和时间
const now = new Date();
// 获取年、月、日,并进行必要的格式化
const day = String(now.getDate()).padStart(2, '0'); // 获取日,并确保是两位数
const month = String(now.getMonth() + 1).padStart(2, '0'); // 获取月并确保是两位数月份是从0开始的所以要加1
const year = String(now.getFullYear()).slice(-2); // 获取年份的最后两位数字
return `${day}${month}${year}`;
}
/**
* 获取当前时分秒
*/
export function formatHhmmss(){
// 创建一个Date对象表示当前日期和时间
const now = new Date();
// 获取当前的小时
const hour = String(now.getHours()).padStart(2, '0');
// 获取当前的分钟
const minute = String(now.getMinutes()).padStart(2, '0');
// 获取当前的秒数
const second = String(now.getSeconds()).padStart(2, '0');
return `${String(Number(hour) - 8).padStart(2, '0')}${minute}${second}`;
}
/**
* 获取当前小时
*/
export function getCurrentHour() {
const now = new Date();
// 获取当前的小时
const hour = String(now.getHours()).padStart(2, '0');
return `${String(Number(hour) - 8).padStart(2, '0')}`;
}