@chenfengyuan/datepicker

中文文档
1.0.10发布于 2020/09/29MITESM + CJS零依赖近 30 天下载 1.7万

A simple jQuery datepicker plugin.

原始使用文档
文档目录63 节
  1. Table of contents
  2. Features
  3. Main
  4. Getting started
  5. Install
  6. Usage
  7. Options
  8. autoShow
  9. autoHide
  10. autoPick
  11. inline
  12. container
  13. trigger
  14. language
  15. format
  16. date
  17. startDate
  18. endDate
  19. startView
  20. weekStart
  21. yearFirst
  22. yearSuffix
  23. days
  24. daysShort
  25. daysMin
  26. months
  27. monthsShort
  28. itemTag
  29. mutedClass
  30. pickedClass
  31. disabledClass
  32. highlightedClass
  33. template
  34. offset
  35. zIndex
  36. filter
  37. show
  38. hide
  39. pick
  40. Methods
  41. show()
  42. hide()
  43. update()
  44. pick()
  45. reset()
  46. getMonthName([month[, short]])
  47. getDayName([day[, short[, min]])
  48. getDate([formatted])
  49. setDate(date)
  50. setStartDate(date)
  51. setEndDate(date)
  52. parseDate(date)
  53. formatDate(date)
  54. destroy()
  55. Events
  56. show.datepicker
  57. hide.datepicker
  58. pick.datepicker
  59. I18n
  60. No conflict
  61. Browser support
  62. Versioning
  63. License

Datepicker

Build Status Downloads Version

A simple jQuery datepicker plugin.

Table of contents

Features

  • Supports options
  • Supports methods
  • Supports events
  • Supports inline mode
  • Supports touch (mobile)
  • Supports internationalization
  • Cross-browser support

Main

dist/
├── datepicker.css
├── datepicker.min.css   (compressed)
├── datepicker.js        (UMD)
├── datepicker.min.js    (UMD, compressed)
├── datepicker.common.js (CommonJS, default)
└── datepicker.esm.js    (ES Module)

Getting started

Install

npm install @chenfengyuan/datepicker

Include files:

<script src="/path/to/jquery.js"></script><!-- jQuery is required -->
<link  href="/path/to/datepicker.css" rel="stylesheet">
<script src="/path/to/datepicker.js"></script>

Usage

Initialize with $.fn.datepicker method.

<input data-toggle="datepicker">
<textarea data-toggle="datepicker"></textarea>
<div data-toggle="datepicker"></div>
$('[data-toggle="datepicker"]').datepicker();

⬆ back to top

Options

You may set datepicker options with $().datepicker(options). If you want to change the global default options, You may use $.fn.datepicker.setDefaults(options).

autoShow

  • Type: Boolean
  • Default: false

Show the datepicker automatically when initialized.

autoHide

  • Type: Boolean
  • Default: false

Hide the datepicker automatically when picked.

autoPick

  • Type: Boolean
  • Default: false

Pick the initial date automatically when initialized.

inline

  • Type: Boolean
  • Default: false

Enable inline mode.

If the element is not an input element, will append the datepicker to the element. If the container option is set, will append the datepicker to the container.

container

  • Type: Element or String(selector)
  • Default: null

A element for putting the datepicker. If not set, the datepicker will be appended to document.body by default.

Only works when the inline option set to true.

trigger

  • Type: Element or String(selector)
  • Default: null

A element for triggering the datepicker.

language

  • Type: String
  • Default: ''

The ISO language code. If not set, will use the built-in language (en-US) by default.

You should define the language first. View the I18n section for more information or check out the i18n folder for available languages.

$().datepicker({
  language: 'en-GB'
});

format

  • Type: String
  • Default: 'mm/dd/yyyy'
  • Available date placeholders:
    • Year: yyyy, yy
    • Month: mm, m
    • Day: dd, d

The date string format.

$().datepicker({
  format: 'yyyy-mm-dd'
});

date

  • Type: Date or String
  • Default: null

The initial date. If not set, will use the current date by default.

$().datepicker({
  date: new Date(2014, 1, 14) // Or '02/14/2014'
});

startDate

  • Type: Date or String
  • Default: null

The start view date. All the dates before this date will be disabled.

endDate

  • Type: Date or String
  • Default: null

The end view date. All the dates after this date will be disabled.

startView

  • Type: Number
  • Default: 0
  • Options:
    • 0: days
    • 1: months
    • 2: years

The start view when initialized.

weekStart

  • Type: Number
  • Default: 0
  • Options:
    • 0: Sunday
    • 1: Monday
    • 2: Tuesday
    • 3: Wednesday
    • 4: Thursday
    • 5: Friday
    • 6: Saturday

The start day of the week.

yearFirst

  • Type: Boolean
  • Default: false

Show year before month on the datepicker header

yearSuffix

  • Type: String
  • Default: ''

A string suffix to the year number.

$().datepicker({
  yearSuffix: '年'
});

days

  • Type: Array
  • Default: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']

Days' name of the week.

daysShort

  • Type: Array
  • Default: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']

Shorter days' name.

daysMin

  • Type: Array
  • Default: ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa']

Shortest days' name.

months

  • Type: Array
  • Default: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']

Months' name.

monthsShort

  • Type: Array
  • Default: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']

Shorter months' name.

itemTag

  • Type: String
  • Default: 'li'

A element tag for each item of years, months and days.

mutedClass

  • Type: String
  • Default: 'muted'

A class (CSS) for muted item.

pickedClass

  • Type: String
  • Default: 'picked'

A class (CSS) for picked item.

disabledClass

  • Type: String
  • Default: 'disabled'

A class (CSS) for disabled item.

highlightedClass

  • Type: String
  • Default: 'highlighted'

A class (CSS) for highlight date item.

template

  • Type: String
  • Default:
<div class="datepicker-container">
  <div class="datepicker-panel" data-view="years picker">
    <ul>
      <li data-view="years prev">&lsaquo;</li>
      <li data-view="years current"></li>
      <li data-view="years next">&rsaquo;</li>
    </ul>
    <ul data-view="years"></ul>
  </div>
  <div class="datepicker-panel" data-view="months picker">
    <ul>
      <li data-view="year prev">&lsaquo;</li>
      <li data-view="year current"></li>
      <li data-view="year next">&rsaquo;</li>
    </ul>
    <ul data-view="months"></ul>
  </div>
  <div class="datepicker-panel" data-view="days picker">
    <ul>
      <li data-view="month prev">&lsaquo;</li>
      <li data-view="month current"></li>
      <li data-view="month next">&rsaquo;</li>
    </ul>
    <ul data-view="week"></ul>
    <ul data-view="days"></ul>
  </div>
</div>

The template of the datepicker.

Note: All the data-view attributes must be set when you customize it.

offset

  • Type: Number
  • Default: 10

The offset top or bottom of the datepicker from the element.

zIndex

  • Type: Number
  • Default: 1

The CSS z-index style for the datepicker.

filter

  • Type: Function
  • Default: null
  • Syntax: filter(date, view)
    • date: the date for checking.
    • view: the the current view, one of day, month or year.

Filter each date item. If return a false value, the related date will be disabled.

var now = Date.now();

$().datepicker({
  filter: function(date, view) {
    if (date.getDay() === 0 && view === 'day') {
      return false; // Disable all Sundays, but still leave months/years, whose first day is a Sunday, enabled.
    }
  }
});

show

  • Type: Function
  • Default: null

A shortcut of the "show.datepicker" event.

hide

  • Type: Function
  • Default: null

A shortcut of the "hide.datepicker" event.

pick

  • Type: Function
  • Default: null

A shortcut of the "pick.datepicker" event.

⬆ back to top

Methods

Common usage:

$().datepicker('method', argument1, , argument2, ..., argumentN);

show()

Show the datepicker.

hide()

Hide the datepicker.

update()

Update the datepicker with the value or text of the current element.

pick()

Pick the current date to the element.

reset()

Reset the datepicker.

getMonthName([month[, short]])

  • month (optional):

    • Type: Number
    • Default: the month of the current date
  • short (optional):

    • Type: Boolean
    • Default: false
    • Get the shorter month name
  • (return value):

    • Type: String

Get the month name with given argument or the current date.

$().datepicker('getMonthName'); // 'January'
$().datepicker('getMonthName', true); // 'Jan'
$().datepicker('getMonthName', 11); // 'December'
$().datepicker('getMonthName', 11, true); // 'Dec'

getDayName([day[, short[, min]])

  • day (optional):

    • Type: Number
    • Default: the day of the current date
  • short (optional):

    • Type: Boolean
    • Default: false
    • Get the shorter day name
  • min (optional):

    • Type: Boolean
    • Default: false
    • Get the shortest day name
  • (return value):

    • Type: String

Get the day name with given argument or the current date.

$().datepicker('getDayName'); // 'Sunday'
$().datepicker('getDayName', true); // 'Sun'
$().datepicker('getDayName', true, true); // 'Su'
$().datepicker('getDayName', 6); // 'Saturday'
$().datepicker('getDayName', 6, true); // 'Sat'
$().datepicker('getDayName', 6, true, true); // 'Sa'

getDate([formatted])

  • formatted (optional):

    • Type: Boolean
    • Default: false
    • Get a formatted date string
  • (return value):

    • Type: Date or String

Get the current date.

$().datepicker('getDate'); // date object
$().datepicker('getDate', true); // '02/14/2014'

setDate(date)

  • date:
    • Type: Date or String

Set the current date with a new date.

$().datepicker('setDate', new Date(2014, 1, 14));
$().datepicker('setDate', '02/14/2014');

setStartDate(date)

  • date:
    • Type: Date or String or null

Set the start view date with a new date.

setEndDate(date)

  • date:
    • Type: Date or String or null

Set the end view date with a new date.

parseDate(date)

  • date:
    • Type: String

Parse a date string with the set date format.

$().datepicker('parseDate', '02/14/2014'); // date object

formatDate(date)

  • date:
    • Type: Date

Format a date object to a string with the set date format.

$().datepicker('formatDate', new Date(2014, 1, 14)); // '02/14/2014'

destroy()

Destroy the datepicker and remove the instance from the target element.

⬆ back to top

Events

show.datepicker

This event fires when starts to show the datepicker.

hide.datepicker

This event fires when starts to hide the datepicker.

pick.datepicker

  • event.date:

    • Type: Date
    • The current date
  • event.view:

    • Type: String
    • Default: ''
    • Options: 'year', 'month', 'day'
    • The current visible view

This event fires when start to pick a year, month or day.

$().on('pick.datepicker', function (e) {
  if (e.date < new Date()) {
    e.preventDefault(); // Prevent to pick the date
  }
});

⬆ back to top

I18n

// datepicker.zh-CN.js
$.fn.datepicker.languages['zh-CN'] = {
  format: 'yyyy年mm月dd日',
  days: ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'],
  daysShort: ['周日', '周一', '周二', '周三', '周四', '周五', '周六'],
  daysMin: ['日', '一', '二', '三', '四', '五', '六'],
  months: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
  monthsShort: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
  weekStart: 1,
  startView: 0,
  yearFirst: true,
  yearSuffix: '年'
};
<script src="/path/to/datepicker.js"></script>
<script src="/path/to/datepicker.zh-CN.js"></script>
<script>
  $().datepicker({
    language: 'zh-CN'
  });
</script>

⬆ back to top

No conflict

If you have to use other plugin with the same namespace, just call the $.fn.datepicker.noConflict method to revert to it.

<script src="other-plugin.js"></script>
<script src="datepicker.js"></script>
<script>
  $.fn.datepicker.noConflict();
  // Code that uses other plugin's "$().datepicker" can follow here.
</script>

Browser support

  • Chrome (latest)
  • Firefox (latest)
  • Safari (latest)
  • Opera (latest)
  • Edge (latest)
  • Internet Explorer 9+

Versioning

Maintained under the Semantic Versioning guidelines.

License

MIT © Chen Fengyuan

⬆ back to top

版本与发布记录

@chenfengyuan/datepicker 共发布 12 个版本,首次发布于 2018/06/30;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.10latest2020/09/296 年前
1.0.92019/09/217 年前
1.0.82019/06/237 年前
1.0.72019/02/197 年前
1.0.62019/01/197 年前
1.0.52019/01/197 年前

关于 @chenfengyuan/datepicker 的常见问题

@chenfengyuan/datepicker 是什么?

A simple jQuery datepicker plugin.

如何安装 @chenfengyuan/datepicker?

在项目目录运行 npm install @chenfengyuan/datepicker,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:jquery(>=1.9.1)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@chenfengyuan/datepicker 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/chenfengyuan__datepicker 是否存在。

@chenfengyuan/datepicker 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@chenfengyuan/datepicker 可以商用吗?

@chenfengyuan/datepicker 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@chenfengyuan/datepicker 的下载量有多少?

根据 npm 官方下载统计,@chenfengyuan/datepicker 在 2026/10/10 – 2026/11/08 这 30 天内被下载 17,311 次,日均约 577 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@chenfengyuan/datepicker 还在维护吗?

最新版本 1.0.10 发布于 2020/09/29(6 年前);至今共发布 12 个版本,其中过去 12 个月发布了 0 个;首次发布于 2018/06/30。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。