百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
C

calendar-base

> 编程语言
开源

使用 JavaScript 生成日历的基本方法

385 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

使用 JavaScript 生成日历的基本方法

Calendar Base

Base methods for generating calendars using JavaScript.

Output is ES5 compatible.

Installation

bash
# using npm
npm install calendar-base

# or yarn
yarn add calendar-base

Usage

javascript
const Calendar = require('calendar-base').Calendar;
const cal = new Calendar();

cal.getCalendar(2020, 0);
/*
Returns an Array with the calendar for January 2020, including empty spaces for
days from the previous and next months:

[
  false,
  false,
  { day: 1, weekDay: 3, month: 0, year: 2020 },
  { day: 2, weekDay: 4, month: 0, year: 2020 },
  { day: 3, weekDay: 5, month: 0, year: 2020 },
  { day: 4, weekDay: 6, month: 0, year: 2020 },
  { day: 5, weekDay: 0, month: 0, year: 2020 },
  ...
]
*/

Check an online example or browse the examples folder for some simple use cases.

Date object notation

Every returned day or date argument follows this notation:

javascript
{
  day: 14,
  month: 9,
  year: 1986,
  weekDay: 4,
  selected: false,
  siblingMonth: false,
  weekNumber: 42
}

Properties month and weekDay respect JavaScript’s Date.prototype.

Only day, month, and year are necessary as input parameters for methods that require a date.

Calendar(options)

Constructor for a new calendar generator.

The object options may have the following properties:

  • startDate: current selected starting date (default undefined)
  • endDate: current selected ending date (default undefined)
  • siblingMonths: whether to include the previous and next months’ days before and after the current month when generating a calendar (default false)
  • weekNumbers: whether to include the week number on each day
  • weekStart: day of the week, respects Date.prototype.getDay (default 0, Sunday)

Calendar.diff(dateOne, dateTwo)

Returns the difference in days between dateOne and dateTwo as a Number.

javascript
> Calendar.diff({ year: 2010, month: 0, day: 1 }, { year: 2010, month: 0, day: 10 });
-9

Calendar.interval(dateOne, dateTwo)

Returns the amount of days between dateOne and dateTwo as a Number.

javascript
> Calendar.interval({ year: 2010, month: 0, day: 1 }, { year: 2010, month: 0, day: 10 });
10

Calendar.compare(leftDate, rightDate)

Compares two date objects, returns:

  • -1 if leftDate rightDate

Useful for quick comparisons such as sorting an array of dates.

javascript
> Calendar.compare({ year: 2010, month: 0, day: 1 }, { year: 2010, month: 0, day: 10 });
1

Calendar.daysInMonth(year, month)

Returns the amount of days in the given month as a Number.

javascript
> Calendar.daysInMonth(2010, 0);
31

Calendar.isLeapYear(year)

Returns whether the given year is a leap year, as a Boolean.

javascript
> Calendar.isLeapYear(2100);
false

Calendar.calculateWeekNumber(date)

Returns the week number for the specified date.

javascript
> Calendar.calculateWeekNumber({year: 1986, month: 9, day: 14 });
42

Calendar.prototype.getCalendar(year, month)

Returns an Array of dates with the days from the given month, always starting at the configured week day.

If sibling months is disabled, paddings are added as false to align the week days, otherwise the respective days from the previous or next months are included.

javascript
> var cal = new Calendar({ siblingMonths: true });
> cal.getCalendar(2015, 5);
[ { day: 31, weekDay: 0, month: 4, year: 2015, siblingMonth: true },
  { day: 1, weekDay: 1, month: 5, year: 2015 },
  { day: 2, weekDay: 2, month: 5, year: 2015 },
  ...
  { day: 4, weekDay: 6, month: 6, year: 2015, siblingMonth: true } ]

Calendar.prototype.setDate(date)

Alias to Calendar.prototype.setStartDate.

Calendar.prototype.setStartDate(date)

Sets the current selected starting date.

javascript
> cal.setStartDate({ year: 2015, month: 0, day: 1 });

Calendar.prototype.setEndDate(date)

Sets the current selected ending date.

javascript
> cal.setEndDate({ year: 2015, month: 0, day: 31 });

Calendar.prototype.isDateSelected(date)

Checks whether the given date is inside the selected dates interval, returns a Boolean.

javascript
> cal.isDateSelected({ year: 2015, month: 0, day: 10 });
true

Important note on week numbers

Week numbers are calculated based on the ISO 8601 standard, which assumes calculations based on weeks starting on Mondays. Be extra careful displaying the week number if your calendar doesn't start on a Monday.

Development

This library uses wes-cli, which simplifies configuration setup. Instead of using yarn install, you should use npx wes-cli install, which will create all configuration files and run yarn install.

Read more about wes-cli.

License

MIT, https://wes.dev/LICENSE.txt

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

TypeScriptcalendarcalendar-generatorjavascriptlibrary

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言