UNPKG

xxm-test-js

Version:
31 lines (30 loc) 1.39 kB
/** * 格式化时间差,将毫秒数转换为易读的时间格式(如"1小时30分钟")。 * 该函数会根据传入的毫秒数,计算并返回相应的时间描述。 * 如果传入的毫秒数无效,将返回错误提示。 * @param {number} milliseconds - 要格式化的时间差(毫秒)。 * @param {Object} [options] - 可选配置项。 * @param {boolean} [options.includeSeconds=false] - 是否包含秒数。 * @param {boolean} [options.compact=false] - 是否使用紧凑格式(如"1h30m")。 * @returns {string} - 格式化后的时间字符串。 * @example * ```js * // 示例1: 格式化1小时30分钟 * const duration1 = 1 * 60 * 60 * 1000 + 30 * 60 * 1000; * console.log(formatDuration(duration1)); // 输出: 1小时30分钟 * * // 示例2: 格式化1小时30分钟15秒,包含秒数 * const duration2 = 1 * 60 * 60 * 1000 + 30 * 60 * 1000 + 15 * 1000; * console.log(formatDuration(duration2, { includeSeconds: true })); // 输出: 1小时30分钟15秒 * * // 示例3: 使用紧凑格式 * console.log(formatDuration(duration1, { compact: true })); // 输出: 1h30m * * // 示例4: 传入无效的毫秒数 * console.log(formatDuration(-1000)); // 输出: 无效的时间差 * ``` */ export declare function formatDuration(milliseconds: number, options?: { includeSeconds?: boolean; compact?: boolean; }): string;