https://github.com/chatwoot/business-hours
A tiny (> 1kb) library to handle business hours on the browser
https://github.com/chatwoot/business-hours
business-hours datetime typescript zero-dependency
Last synced: 11 months ago
JSON representation
A tiny (> 1kb) library to handle business hours on the browser
- Host: GitHub
- URL: https://github.com/chatwoot/business-hours
- Owner: chatwoot
- License: mit
- Created: 2024-05-24T10:35:32.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-06-07T13:29:07.000Z (almost 2 years ago)
- Last Synced: 2025-06-07T00:11:30.850Z (12 months ago)
- Topics: business-hours, datetime, typescript, zero-dependency
- Language: TypeScript
- Homepage: https://paka.dev/npm/@chatwoot/business-hours
- Size: 89.8 KB
- Stars: 12
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Business Hours
## Installation
```sh
pnpm install @chatwoot/business-hours
```
```sh
npm install @chatwoot/business-hours
```
```sh
yarn add @chatwoot/business-hours
```
## Usage
You need to initialize the scheduler using the following options, the hours defines the schedule for each day of the week, and the holidays is an array of `Date` objects.
```typescript
import { Scheduler } from '@chatwoot/business-hours'
const scheduler = new Scheduler({
hours: {
0: null,
1: [
{ start: "09:00", end: "12:00" },
{ start: "13:00", end: "18:00" },
],
2: [
{ start: "09:00", end: "12:00" },
{ start: "13:00", end: "18:00" },
],
3: [{ start: "09:00", end: "12:00" }],
4: [{ start: "09:00", end: "18:00" }],
5: [{ start: "09:00", end: "18:00" }],
6: null,
},
holidays: [
new Date("2024-03-08"),
new Date("2024-03-09"),
new Date("2024-03-10"),
],
});
```
## Methods available
### `nextWorkingTime`
This method takes a date and returns the next working time. If the date is a holiday, it will return the next working day. If the date is a working day, it will return the next working time.
```js
scheduler.nextWorkingTime(new Date("2024-03-08"));
```
### `nextWorkingDay`
This method takes a date and returns the next working day. If the date is a holiday, it will return the next working day. If the date is a working day, it will return the same day.
```js
scheduler.nextWorkingDay(new Date("2024-03-08"));
```
### `isWorkingDay`
This method takes a date and returns a boolean. If the date is a holiday, it will return false. If the date is a working day, it will return true.
```js
scheduler.isWorkingDay(new Date("2024-03-08"));
```
### `isWorkingTime`
This method takes a date and returns a boolean. If the date is a holiday, it will return false. If the date is a working day, it will return true.
```js
scheduler.isWorkingTime(new Date("2024-03-08"));
```
### `isHoliday`
This method takes a date and returns a boolean. If the date is a holiday, it will return true. If the date is a working day, it will return false.
```js
scheduler.isHoliday(new Date("2024-03-08"));
```
### `addTime`
This method takes a datetime, along with the time to add in seconds and returns the new date. If the date is outside working hours, it will add it to the working hours, suppose the working hours is between 8AM to 5PM, and I pass it 7:30 PM and ask it to add 10 mins, it will return 8:10 AM.
```js
scheduler.addTime(new Date("2024-03-08"), 3600);
```