Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions .github/workflows/deploy-docs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: Deploy VitePress site to GitHub Pages

on:
push:
branches:
- master
paths:
- 'docs/**'
- '.github/workflows/deploy-docs.yml'
workflow_dispatch:

permissions:
contents: read
pages: write
id-token: write

concurrency:
group: pages
cancel-in-progress: false

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 18
cache: npm

- name: Setup Pages
uses: actions/configure-pages@v4

- name: Install dependencies
run: npm ci

- name: Build with VitePress
run: npm run docs:build

- name: Upload artifact
uses: actions/upload-pages-artifact@v3
with:
path: docs/.vitepress/dist

deploy:
needs: build
runs-on: ubuntu-latest
permissions:
pages: write
id-token: write
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.DS_Store
.claude/
CLAUDE.md
cache/
coverage/
dist/
node_modules/
node_modules/
90 changes: 90 additions & 0 deletions docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { defineConfig } from 'vitepress';

export default defineConfig({
title: 'date-and-time',
description: 'The simplest, most intuitive date and time library',
base: '/date-and-time/',
ignoreDeadLinks: true,

head: [
['link', { rel: 'icon', href: '/date-and-time/favicon.ico' }]
],

themeConfig: {
logo: '/logo.png',

nav: [
{ text: 'Guide', link: '/guide/' },
{ text: 'API Reference', link: '/api/' },
{ text: 'Locales', link: '/locales' },
{ text: 'Timezones', link: '/timezones' },
{ text: 'Plugins', link: '/plugins' },
{ text: 'Migration', link: '/migration' },
{
text: 'Links',
items: [
{ text: 'GitHub', link: 'https://github.com/knowledgecode/date-and-time' },
{ text: 'npm', link: 'https://www.npmjs.com/package/date-and-time' }
]
}
],

sidebar: {
'/guide/': [
{
text: 'Getting Started',
items: [
{ text: 'Introduction', link: '/guide/' },
{ text: 'Installation', link: '/guide/installation' },
{ text: 'Quick Start', link: '/guide/quick-start' }
]
}
],
'/api/': [
{
text: 'Core Functions',
items: [
{ text: 'Overview', link: '/api/' },
{ text: 'format()', link: '/api/format' },
{ text: 'parse()', link: '/api/parse' },
{ text: 'compile()', link: '/api/compile' },
{ text: 'preparse()', link: '/api/preparse' },
{ text: 'isValid()', link: '/api/isValid' },
{ text: 'transform()', link: '/api/transform' },
{ text: 'addYears()', link: '/api/addYears' },
{ text: 'addMonths()', link: '/api/addMonths' },
{ text: 'addDays()', link: '/api/addDays' },
{ text: 'addHours()', link: '/api/addHours' },
{ text: 'addMinutes()', link: '/api/addMinutes' },
{ text: 'addSeconds()', link: '/api/addSeconds' },
{ text: 'addMilliseconds()', link: '/api/addMilliseconds' },
{ text: 'subtract()', link: '/api/subtract' },
{ text: 'isLeapYear()', link: '/api/isLeapYear' },
{ text: 'isSameDay()', link: '/api/isSameDay' },
]
}
]
},

socialLinks: [
{ icon: 'github', link: 'https://github.com/knowledgecode/date-and-time' }
],

footer: {
message: 'Released under the MIT License.',
copyright: 'Copyright © 2015 KNOWLEDGECODE'
},

search: {
provider: 'local'
}
},

markdown: {
theme: {
light: 'github-light',
dark: 'github-dark'
},
lineNumbers: true
}
})
128 changes: 128 additions & 0 deletions docs/api/addDays.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# addDays()

Adds or subtracts days from a Date object. Handles month boundaries, leap years, and daylight saving time transitions properly.

## Syntax

```typescript
addDays(dateObj, days[, timeZone])
```

### Parameters

| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| `dateObj` | `Date` | Yes | The base Date object |
| `days` | `number` | Yes | Number of days to add (positive) or subtract (negative) |
| `timeZone` | `TimeZone \| 'UTC'` | No | Timezone for the calculation |

### Returns

`Date` - A new Date object with the specified number of days added or subtracted

## Basic Examples

### Adding and Subtracting Days

```typescript
import { addDays } from 'date-and-time';

const date = new Date(2024, 7, 15); // August 15, 2024

// Add days
const future = addDays(date, 10);
console.log(future); // August 25, 2024

// Subtract days
const past = addDays(date, -5);
console.log(past); // August 10, 2024
```

### Daylight Saving Time Aware Calculations

```typescript
import { addDays } from 'date-and-time';
import New_York from 'date-and-time/timezones/America/New_York';

// Working with specific timezones
const nyDate = new Date('2024-03-10T05:00:00Z'); // March 10, 2024 05:00 UTC (DST transition day)

// Add 30 days in New York timezone
const futureNY = addDays(nyDate, 30, New_York);
console.log(futureNY); // April 9, 2024 04:00 UTC (EDT, DST adjusted)

// UTC calculation for comparison
const futureUTC = addDays(nyDate, 30, 'UTC');
console.log(futureUTC); // April 9, 2024 05:00 UTC (same time, no DST adjustment)
```

## Use Cases

### Work Day Calculations

```typescript
function addBusinessDays(date: Date, businessDays: number): Date {
let result = new Date(date);
let daysToAdd = businessDays;

while (daysToAdd > 0) {
result = addDays(result, 1);
const dayOfWeek = result.getDay();

// Skip weekends (0 = Sunday, 6 = Saturday)
if (dayOfWeek !== 0 && dayOfWeek !== 6) {
daysToAdd--;
}
}

return result;
}

const friday = new Date(2024, 7, 23); // August 23, 2024 (Friday)
const nextBusinessDay = addBusinessDays(friday, 1);
console.log(nextBusinessDay); // August 26, 2024 (Monday)
```

### Date Range Generation

```typescript
function generateDateRange(startDate: Date, endDate: Date): Date[] {
const dates: Date[] = [];
let currentDate = new Date(startDate);

while (currentDate <= endDate) {
dates.push(new Date(currentDate));
currentDate = addDays(currentDate, 1);
}

return dates;
}

const start = new Date(2024, 7, 28); // August 28, 2024
const end = new Date(2024, 8, 3); // September 3, 2024
const dateRange = generateDateRange(start, end);
console.log(dateRange);
// [Aug 28, Aug 29, Aug 30, Aug 31, Sep 1, Sep 2, Sep 3]
```

## Immutability

`addDays()` does not modify the original Date object:

```typescript
const originalDate = new Date(2024, 7, 15);
const modifiedDate = addDays(originalDate, 10);

console.log(originalDate); // August 15, 2024 (unchanged)
console.log(modifiedDate); // August 25, 2024 (new object)
```

## See Also

- [`addYears()`](./addYears) - Add/subtract years
- [`addMonths()`](./addMonths) - Add/subtract months
- [`addHours()`](./addHours) - Add/subtract hours
- [`addMinutes()`](./addMinutes) - Add/subtract minutes
- [`addSeconds()`](./addSeconds) - Add/subtract seconds
- [`addMilliseconds()`](./addMilliseconds) - Add/subtract milliseconds
- [`subtract()`](./subtract) - Calculate differences with Duration objects
Loading