date-manip
Version:
A lightweight JavaScript date utility library that provides modularity, high performance, and additional features. It supports various date operations, including date addition and subtraction, formatting, comparison, etc.
38 lines (37 loc) • 1.66 kB
TypeScript
import { DateInput } from './types';
/**
* Parses a date from various input types.
* 从各种输入类型中解析日期。
*
* @param input - The input to parse, which can be a string, number, Date, or array of numbers.
* (要解析的输入,可以是字符串、数字、Date 对象或数字数组。)
* @param format - An optional format string that specifies the format of the input string if the input is a string.
* Or A boolean indicating whether the date should be in UTC.
* (可选的格式字符串,指定输入字符串的格式,如果输入是字符串。 或者是布尔值,表示日期是否应为 UTC。)
* @returns A Date object representing the parsed date. (表示解析后日期的 Date 对象。)
* @example
* ```ts
* // 从字符串中解析日期
* const result = parse('20231001123456', 'YYYYMMDDHHmmss');
* console.log(result); // 输出: 2023-10-01T12:34:56.000Z
*
* // 从 ISO 8601 字符串中解析日期
* const isoResult = parse('2023-10-01T12:34:56Z');
* console.log(isoResult); // 输出: 2023-10-01T12:34:56.000Z
*
* // 从数字数组中解析日期
* const arrayResult = parse([2023, 9, 1, 12, 34, 56]);
* console.log(arrayResult); // 输出: 2023-10-01T12:34:56.000Z
*
* // 从数字中解析日期
* const numberResult = parse(1696119296000);
* console.log(numberResult); // 输出: 2023-10-01T12:34:56.000Z
*
* // 从 Date 对象中解析日期
* const dateResult = parse(new Date('2023-10-01T12:34:56Z'));
* console.log(dateResult); // 输出: 2023-10-01T12:34:56.000Z
* ```
*/
export default function parse(input: DateInput | {
isValid: () => any;
}, format?: string): Date;