UNPKG

1.88 kBTypeScriptView Raw
1import type { ContextOptions, DateArg, DateValues } from "./types.js";
2/**
3 * The {@link set} function options.
4 */
5export interface SetOptions<DateType extends Date = Date>
6 extends ContextOptions<DateType> {}
7/**
8 * @name set
9 * @category Common Helpers
10 * @summary Set date values to a given date.
11 *
12 * @description
13 * Set date values to a given date.
14 *
15 * Sets time values to date from object `values`.
16 * A value is not set if it is undefined or null or doesn't exist in `values`.
17 *
18 * Note about bundle size: `set` does not internally use `setX` functions from date-fns but instead opts
19 * to use native `Date#setX` methods. If you use this function, you may not want to include the
20 * other `setX` functions that date-fns provides if you are concerned about the bundle size.
21 *
22 * @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).
23 * @typeParam ResultDate - The result `Date` type, it is the type returned from the context function if it is passed, or inferred from the arguments.
24 *
25 * @param date - The date to be changed
26 * @param values - The date values to be set
27 * @param options - The options
28 *
29 * @returns The new date with options set
30 *
31 * @example
32 * // Transform 1 September 2014 into 20 October 2015 in a single line:
33 * const result = set(new Date(2014, 8, 20), { year: 2015, month: 9, date: 20 })
34 * //=> Tue Oct 20 2015 00:00:00
35 *
36 * @example
37 * // Set 12 PM to 1 September 2014 01:23:45 to 1 September 2014 12:00:00:
38 * const result = set(new Date(2014, 8, 1, 1, 23, 45), { hours: 12 })
39 * //=> Mon Sep 01 2014 12:23:45
40 */
41export declare function set<
42 DateType extends Date,
43 ResultDate extends Date = DateType,
44>(
45 date: DateArg<DateType>,
46 values: DateValues,
47 options?: SetOptions<ResultDate>,
48): ResultDate;