| 123456789101112131415161718192021222324252627282930313233 |
- export function formatDateTime(date:any) {
- const year = date.getFullYear(); // 获取年份
- const month = String(date.getMonth() + 1).padStart(2, '0'); // 获取月份
- const day = String(date.getDate()).padStart(2, '0'); // 获取日期
- const hours = String(date.getHours()).padStart(2, '0'); // 获取小时
- const minutes = String(date.getMinutes()).padStart(2, '0'); // 获取分钟
- const seconds = String(date.getSeconds()).padStart(2, '0'); // 获取秒钟
- return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
- }
- /*
- *计算时间与当前的小时
- */
- export function getTimeHours(date: Date | string): number | undefined {
- if (!date) return undefined;
- const targetTime = new Date(date);
- if (isNaN(targetTime.getTime())) return undefined; // 检查日期是否有效
- const currentTime = new Date();
- const timeDiff = currentTime.getTime() - targetTime.getTime(); // 获取毫秒差
- return Math.floor(timeDiff / (1000 * 60 * 60)); // 转换为小时
- }
- /*
- *获取当前时间时分秒
- */
- export function getCurrentTime() {
- const now = new Date();
- const hours = String(now.getHours()).padStart(2, '0');
- const minutes = String(now.getMinutes()).padStart(2, '0');
- const seconds = String(now.getSeconds()).padStart(2, '0');
- return `${hours}:${minutes}:${seconds}`;
- };
|