38 lines
1.1 KiB
JavaScript
38 lines
1.1 KiB
JavaScript
import dayjs from 'dayjs';
|
|
import timezone from 'dayjs/plugin/timezone';
|
|
import utc from 'dayjs/plugin/utc';
|
|
|
|
// Configure dayjs
|
|
dayjs.extend(utc);
|
|
dayjs.extend(timezone);
|
|
|
|
/**
|
|
* Format date to locale string with timezone
|
|
* @param {string|Date} date - The date to format
|
|
* @returns {string} Formatted date string
|
|
*/
|
|
export function formatDate(date) {
|
|
return dayjs.tz(date, 'Asia/Shanghai').format('YYYY-MM-DD HH:mm:ss');
|
|
}
|
|
|
|
/**
|
|
* Format seconds to readable duration string
|
|
* @param {number} seconds - Number of seconds
|
|
* @returns {string} Formatted duration string (e.g., "2h 30m 15s")
|
|
*/
|
|
export function formatDuration(seconds) {
|
|
if (!seconds || seconds < 0) return '0 秒';
|
|
|
|
const hours = Math.floor(seconds / 3600);
|
|
const minutes = Math.floor((seconds % 3600) / 60);
|
|
const remainingSeconds = Math.floor(seconds % 60);
|
|
|
|
const parts = [];
|
|
if (hours > 0) parts.push(`${hours} 时`);
|
|
if (minutes > 0) parts.push(`${minutes} 分`);
|
|
if (remainingSeconds > 0 || parts.length === 0) parts.push(`${remainingSeconds} 秒`);
|
|
|
|
return parts.join(' ');
|
|
}
|
|
|