|
| 1 | +# addDays() |
| 2 | + |
| 3 | +Adds or subtracts days from a Date object. Handles month boundaries, leap years, and daylight saving time transitions properly. |
| 4 | + |
| 5 | +## Syntax |
| 6 | + |
| 7 | +```typescript |
| 8 | +addDays(dateObj, days[, timeZone]) |
| 9 | +``` |
| 10 | + |
| 11 | +### Parameters |
| 12 | + |
| 13 | +| Parameter | Type | Required | Description | |
| 14 | +|-----------|------|----------|-------------| |
| 15 | +| `dateObj` | `Date` | Yes | The base Date object | |
| 16 | +| `days` | `number` | Yes | Number of days to add (positive) or subtract (negative) | |
| 17 | +| `timeZone` | `TimeZone \| 'UTC'` | No | Timezone for the calculation | |
| 18 | + |
| 19 | +### Returns |
| 20 | + |
| 21 | +`Date` - A new Date object with the specified number of days added or subtracted |
| 22 | + |
| 23 | +## Basic Examples |
| 24 | + |
| 25 | +### Adding and Subtracting Days |
| 26 | + |
| 27 | +```typescript |
| 28 | +import { addDays } from 'date-and-time'; |
| 29 | + |
| 30 | +const date = new Date(2024, 7, 15); // August 15, 2024 |
| 31 | + |
| 32 | +// Add days |
| 33 | +const future = addDays(date, 10); |
| 34 | +console.log(future); // August 25, 2024 |
| 35 | + |
| 36 | +// Subtract days |
| 37 | +const past = addDays(date, -5); |
| 38 | +console.log(past); // August 10, 2024 |
| 39 | +``` |
| 40 | + |
| 41 | +### Daylight Saving Time Aware Calculations |
| 42 | + |
| 43 | +```typescript |
| 44 | +import { addDays } from 'date-and-time'; |
| 45 | +import New_York from 'date-and-time/timezones/America/New_York'; |
| 46 | + |
| 47 | +// Working with specific timezones |
| 48 | +const nyDate = new Date('2024-03-10T05:00:00Z'); // March 10, 2024 05:00 UTC (DST transition day) |
| 49 | + |
| 50 | +// Add 30 days in New York timezone |
| 51 | +const futureNY = addDays(nyDate, 30, New_York); |
| 52 | +console.log(futureNY); // April 9, 2024 04:00 UTC (EDT, DST adjusted) |
| 53 | + |
| 54 | +// UTC calculation for comparison |
| 55 | +const futureUTC = addDays(nyDate, 30, 'UTC'); |
| 56 | +console.log(futureUTC); // April 9, 2024 05:00 UTC (same time, no DST adjustment) |
| 57 | +``` |
| 58 | + |
| 59 | +## Use Cases |
| 60 | + |
| 61 | +### Work Day Calculations |
| 62 | + |
| 63 | +```typescript |
| 64 | +function addBusinessDays(date: Date, businessDays: number): Date { |
| 65 | + let result = new Date(date); |
| 66 | + let daysToAdd = businessDays; |
| 67 | + |
| 68 | + while (daysToAdd > 0) { |
| 69 | + result = addDays(result, 1); |
| 70 | + const dayOfWeek = result.getDay(); |
| 71 | + |
| 72 | + // Skip weekends (0 = Sunday, 6 = Saturday) |
| 73 | + if (dayOfWeek !== 0 && dayOfWeek !== 6) { |
| 74 | + daysToAdd--; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return result; |
| 79 | +} |
| 80 | + |
| 81 | +const friday = new Date(2024, 7, 23); // August 23, 2024 (Friday) |
| 82 | +const nextBusinessDay = addBusinessDays(friday, 1); |
| 83 | +console.log(nextBusinessDay); // August 26, 2024 (Monday) |
| 84 | +``` |
| 85 | + |
| 86 | +### Date Range Generation |
| 87 | + |
| 88 | +```typescript |
| 89 | +function generateDateRange(startDate: Date, endDate: Date): Date[] { |
| 90 | + const dates: Date[] = []; |
| 91 | + let currentDate = new Date(startDate); |
| 92 | + |
| 93 | + while (currentDate <= endDate) { |
| 94 | + dates.push(new Date(currentDate)); |
| 95 | + currentDate = addDays(currentDate, 1); |
| 96 | + } |
| 97 | + |
| 98 | + return dates; |
| 99 | +} |
| 100 | + |
| 101 | +const start = new Date(2024, 7, 28); // August 28, 2024 |
| 102 | +const end = new Date(2024, 8, 3); // September 3, 2024 |
| 103 | +const dateRange = generateDateRange(start, end); |
| 104 | +console.log(dateRange); |
| 105 | +// [Aug 28, Aug 29, Aug 30, Aug 31, Sep 1, Sep 2, Sep 3] |
| 106 | +``` |
| 107 | + |
| 108 | +## Immutability |
| 109 | + |
| 110 | +`addDays()` does not modify the original Date object: |
| 111 | + |
| 112 | +```typescript |
| 113 | +const originalDate = new Date(2024, 7, 15); |
| 114 | +const modifiedDate = addDays(originalDate, 10); |
| 115 | + |
| 116 | +console.log(originalDate); // August 15, 2024 (unchanged) |
| 117 | +console.log(modifiedDate); // August 25, 2024 (new object) |
| 118 | +``` |
| 119 | + |
| 120 | +## See Also |
| 121 | + |
| 122 | +- [`addYears()`](./addYears) - Add/subtract years |
| 123 | +- [`addMonths()`](./addMonths) - Add/subtract months |
| 124 | +- [`addHours()`](./addHours) - Add/subtract hours |
| 125 | +- [`addMinutes()`](./addMinutes) - Add/subtract minutes |
| 126 | +- [`addSeconds()`](./addSeconds) - Add/subtract seconds |
| 127 | +- [`addMilliseconds()`](./addMilliseconds) - Add/subtract milliseconds |
| 128 | +- [`subtract()`](./subtract) - Calculate differences with Duration objects |
0 commit comments