UNPKG

1.78 kBTypeScriptView Raw
1import type { ConstructableDate, ContextFn, DateArg } from "./types.js";
2/**
3 * @name toDate
4 * @category Common Helpers
5 * @summary Convert the given argument to an instance of Date.
6 *
7 * @description
8 * Convert the given argument to an instance of Date.
9 *
10 * If the argument is an instance of Date, the function returns its clone.
11 *
12 * If the argument is a number, it is treated as a timestamp.
13 *
14 * If the argument is none of the above, the function returns Invalid Date.
15 *
16 * Starting from v3.7.0, it clones a date using `[Symbol.for("constructDateFrom")]`
17 * enabling to transfer extra properties from the reference date to the new date.
18 * It's useful for extensions like [`TZDate`](https://github.com/date-fns/tz)
19 * that accept a time zone as a constructor argument.
20 *
21 * **Note**: *all* Date arguments passed to any *date-fns* function is processed by `toDate`.
22 *
23 * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
24 * @typeParam ResultDate - The result `Date` type, it is the type returned from the context function if it is passed, or inferred from the arguments.
25 *
26 * @param argument - The value to convert
27 *
28 * @returns The parsed date in the local time zone
29 *
30 * @example
31 * // Clone the date:
32 * const result = toDate(new Date(2014, 1, 11, 11, 30, 30))
33 * //=> Tue Feb 11 2014 11:30:30
34 *
35 * @example
36 * // Convert the timestamp to date:
37 * const result = toDate(1392098430000)
38 * //=> Tue Feb 11 2014 11:30:30
39 */
40export declare function toDate<
41 DateType extends Date | ConstructableDate,
42 ResultDate extends Date = DateType,
43>(
44 argument: DateArg<DateType>,
45 context?: ContextFn<ResultDate> | undefined,
46): ResultDate;