Pikaday.js 是一个 JavaScript 日期选择器,它不依赖于任何第三方 JavaScript 库,并且文件大小小于 5K,但是功能却一点不弱,可以进行很多高级定制。并且样式可以根据 CSS 进行更改选择器的设计,当然默认的样式已经非常不错了。

特点

重量轻插件(小于5kb压缩和gzip)

没有依赖(如果需要格式化日期,可能需要使用到 Moment.js)

模块化CSS类,方便自定义样式。

使用方法

首先创建如下的输入框:

在页脚加载 Pikaday 的 JavaScript 库和 CSS 文件,并调用 Pikaday:

var picker = new Pikaday({ field: document.getElementById('datepicker') });

如果网页已经加载了 jQuery 库,其中调用代码可以改成下面更简洁的方式:

var picker = new Pikaday({ field: $('#datepicker')[0] });

如果pikaday实例未绑定到任何一个元素,你可以添加元素到任何地方:

var field = document.getElementById('datepicker');

var picker = new Pikaday({

onSelect: function(date) {

field.value = picker.toString();

}

});

field.parentNode.insertBefore(picker.el, field.nextSibling);

通过 Moment.js 高级格式化Pikaday.js日期的显示格式,你可以点击这里查看更多 Moment.js 的示例。

var picker = new Pikaday({

field: document.getElementById('datepicker'),

format: 'D MMM YYYY',

onSelect: function() {

console.log(this.getMoment().format('Do MMMM YYYY'));

}

});

配置参数

As the examples demonstrate above Pikaday has many useful options:

field bind the datepicker to a form field

trigger use a different element to trigger opening the datepicker, see trigger example (default to field)

bound automatically show/hide the datepicker on field focus (default true if field is set)

position preferred position of the datepicker relative to the form field, e.g.: top right, bottom right Note: automatic adjustment may occur to avoid datepicker from being displayed outside the viewport, see positions example (default to ‘bottom left’)

reposition can be set to false to not reposition datepicker within the viewport, forcing it to take the configured position (default: true)

container DOM node to render calendar into, see container example (default: undefined)

format the default output format for .toString() and field value (requires Moment.js for custom formatting)

formatStrict the default flag for moment’s strict date parsing (requires Moment.js for custom formatting)

defaultDate the initial date to view when first opened

setDefaultDate make the defaultDate the initial selected value

firstDay first day of the week (0: Sunday, 1: Monday, etc)

minDate the minimum/earliest date that can be selected (this should be a native Date object – e.g. new Date() or moment().toDate())

maxDate the maximum/latest date that can be selected (this should be a native Date object – e.g. new Date() or moment().toDate())

disableWeekends disallow selection of Saturdays or Sundays

disableDayFn callback function that gets passed a Date object for each day in view. Should return true to disable selection of that day.

yearRange number of years either side (e.g. 10) or array of upper/lower range (e.g. [1900,2015])

showWeekNumber show the ISO week number at the head of the row (default false)

pickWholeWeek select a whole week instead of a day (default false)

isRTL reverse the calendar for right-to-left languages

i18n language defaults for month and weekday names (see internationalization below)

yearSuffix additional text to append to the year in the title

showMonthAfterYear render the month after year in the title (default false)

showDaysInNextAndPreviousMonths render days of the calendar grid that fall in the next or previous months to the current month instead of rendering an empty table cell (default: false)

numberOfMonths number of visible calendars

mainCalendar when numberOfMonths is used, this will help you to choose where the main calendar will be (default left, can be set to right). Only used for the first display or when a selected date is not already visible

events array of dates that you would like to differentiate from regular days (e.g. ['Sat Jun 28 2017', 'Sun Jun 29 2017', 'Tue Jul 01 2017',])

theme define a classname that can be used as a hook for styling different themes, see theme example (default null)

blurFieldOnSelect defines if the field is blurred when a date is selected (default true)

onSelect callback function for when a date is selected

onOpen callback function for when the picker becomes visible

onClose callback function for when the picker is hidden

onDraw callback function for when the picker draws a new month

jQuery Plugin

The normal version of Pikaday does not require jQuery, however there is a jQuery plugin if that floats your boat (see plugins/pikaday.jquery.js in the repository). This version requires jQuery, naturally, and can be used like other plugins: See the jQuery example for a full version.

// activate datepickers for all elements with a class of `datepicker`

$('.datepicker').pikaday({ firstDay: 1 });

// chain a few methods for the first datepicker, jQuery style!

$('.datepicker').eq(0).pikaday('show').pikaday('gotoYear', 2042);

AMD support

If you use a modular script loader than Pikaday is not bound to the global object and will fit nicely in your build process. You can require Pikaday just like any other module. See the AMD example for a full version.

require(['pikaday'], function(Pikaday) {

var picker = new Pikaday({ field: document.getElementById('datepicker') });

});

The same applies for the jQuery plugin mentioned above. See the jQuery AMD example for a full version.

require(['jquery', 'pikaday.jquery'], function($) {

$('#datepicker').pikaday();

});

CommonJS module support

If you use a CommonJS compatible environment you can use the require function to import Pikaday.

var pikaday = require('pikaday');

When you bundle all your required modules with Browserify and you don’t use Moment.js specify the ignore option:

browserify main.js -o bundle.js -i moment

Ruby on Rails

If you’re using Ruby on Rails, make sure to check out the Pikaday gem.

Methods

You can control the date picker after creation:

var picker = new Pikaday({ field: document.getElementById('datepicker') });

Get and set date

picker.toString('YYYY-MM-DD')

Returns the selected date in a string format. If Moment.js exists (recommended) then Pikaday can return any format that Moment understands, otherwise you’re stuck with JavaScript’s default.

picker.getDate()

Returns a basic JavaScript Date object of the selected day, or null if no selection.

picker.setDate('2015-01-01')

Set the current selection. This will be restricted within the bounds of minDate and maxDate options if they’re specified. You can optionally pass a boolean as the second parameter to prevent triggering of the onSelect callback (true), allowing the date to be set silently.

picker.getMoment()

Returns a Moment.js object for the selected date (Moment must be loaded before Pikaday).

picker.setMoment(moment('14th February 2014', 'DDo MMMM YYYY'))

Set the current selection with a Moment.js object (see setDate for details).

Change current view

picker.gotoDate(new Date(2014, 1))

Change the current view to see a specific date. This example will jump to February 2014 (month is a zero-based index).

picker.gotoToday()

Shortcut for picker.gotoDate(new Date())

picker.gotoMonth(2)

Change the current view by month (0: January, 1: Februrary, etc).

picker.nextMonth() picker.prevMonth()

Go to the next or previous month (this will change year if necessary).

picker.gotoYear()

Change the year being viewed.

picker.setMinDate()

Update the minimum/earliest date that can be selected.

picker.setMaxDate()

Update the maximum/latest date that can be selected.

picker.setStartRange()

Update the range start date. For using two Pikaday instances to select a date range.

picker.setEndRange()

Update the range end date. For using two Pikaday instances to select a date range.

Show and hide datepicker

picker.isVisible()

Returns true or false.

picker.show()

Make the picker visible.

picker.adjustPosition()

Recalculate and change the position of the picker.

picker.hide()

Hide the picker making it invisible.

picker.destroy()

Hide the picker and remove all event listeners — no going back!

Internationalization

The default i18n configuration format looks like this:

i18n: {

previousMonth : 'Previous Month',

nextMonth : 'Next Month',

months : ['January','February','March','April','May','June','July','August','September','October','November','December'],

weekdays : ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'],

weekdaysShort : ['Sun','Mon','Tue','Wed','Thu','Fri','Sat']

}

You must provide 12 months and 7 weekdays (with abbreviations). Always specify weekdays in this order with Sunday first. You can change the firstDay option to reorder if necessary (0: Sunday, 1: Monday, etc). You can also set isRTL to true for languages that are read right-to-left.

浏览器兼容

IE 7+

Chrome 8+

Firefox 3.5+

Safari 3+

Opera 10.6+

相关链接

ajax小型日期插件,Pikaday.js简约轻量级的日期选择插件 - 资源分享相关推荐

  1. vue前端表格插件_Grid.js - 跨框架的前端表格插件

    只想简简单单画个表格,但 React,Vue,Angular,-,这么多前端框架,各自都有不同的表格渲染库.就没有表格库能"一次画表,到处运行"吗?来看看 Grid.js 这个跨框 ...

  2. html显示日期时间代码,JS全中文显示日期时间代码

    JS全中文显示日期时间代码_网页代码站(www.webdm.cn) function number(index1){ var numberstring="一二三四五六七八九十"; ...

  3. html页面酒店日历插件,基于vue2.x的酒店日历选择插件

    基于vue2.x的酒店日历选择插件 效果图 快速使用安装: npm install -S vue-hotel-calendar 或者 yarn add vue-hotel-calendar 使用: i ...

  4. html5怎么兼容js 插件,Modernizr.js入门指南(HTML5CSS3浏览器兼容插件)

    HTML5 和 CSS3 的快速发展,给我们带来了极大的便利,比如从此再也不用花费大量的时间只是为了设计一个圆角的效果. 但是!我们不能像控制机器一样来控制所有的人都一夜之间升级到现代浏览器,因为那些 ...

  5. JS格式化日期、Javascript格式化日期对象、JS时间戳转化为日期对象

    函数参数说明: formmatDate(参数1,参数2): 参数1:日期对象,不能直接传入时间戳 参数2:指定转化的日期格式 注:可以使用new Date('时间戳')转化为普通日期对象 例如: ne ...

  6. 谷歌地图插件Mapsed.js

    我们在一些WEB项目中需要应用简单的地图,而且最好是可以自定义标注地点,最好是可以从本地数据库中读取并在地图上展示地点,那么谷歌地图插件Mapsed.js是比较好的选择,使用起来简单,无需注册地图接口 ...

  7. 省市县插件PCASClass.js的基本使用方法

    省市县插件PCASClass.js的基本使用方法 引入插件 在head标签内引入下面代码 <script type="text/javascript" src="j ...

  8. 点击左右有缝轮播html,超帅轮播插件tabstools.js教程之实现数字+箭头+多栏轮播

    摘要: 前面我们讲了tabstools.js插件可实现多种轮播,如:选项卡轮播.数字轮播.缩略图轮播.卡盘轮播.等等,今天我们再来了解下多栏轮播效果...... 万万没想到!写这篇文章竞是在两个月之后 ...

  9. html中国家的下拉列表,jQuery Select下拉列表国家选择插件

    jQuery Select下拉国家选择插件简介 本文提供JQuery国家选择插件制作select下拉框带搜索功能,和图标的下拉国家列表选择插件代码.支持搜索快速查找,带国旗的国家下拉选择插件! jQu ...

最新文章

  1. 用了 Elasticsearch 后,查询起飞了!
  2. 让我为你介绍一个神器:Chimee,一套可扩展的 H5 视频播放器解决方案
  3. Chrome浏览器切换到之前打开的标签页会重新加载
  4. 012-- mysql的分区和分表
  5. 从键盘读取数据,回车才能显示的问题
  6. ios 中的tintColor
  7. 自由幻想java_新手学习Java之面对对象-----继承
  8. 一、tkinter简介
  9. 财务分析思维导图模板分享
  10. 手机端html怎么复制到剪贴板,移动端和pc端的复制到剪贴板功能
  11. linux更新opengl驱动下载,支持OpenGL 3.2 NVIDIA全新Linux驱动发布
  12. 博客园编辑器为Markdown时改变图片大小
  13. 如何建立高绩效的团队
  14. 微信小程序之生成海报保存本地
  15. Cadence Allegro倒角图文教程及视频演示
  16. Java中 关键字abstract(抽像)的定义
  17. linux下无论什么命令都command not fount
  18. 提权-Windows操作系统
  19. JavaWeb(华清远见)
  20. springcache使用详解

热门文章

  1. 在复杂场景下基于VIO辅助的运动恢复结构方案
  2. python开发职位_【python开发岗位职责|python开发是做什么的】-看准网
  3. 洛阳鼓励农民进城定居
  4. HTTP 请求方法大全| HTTP Request Method
  5. 虚拟机映射、虚拟机与物理机共享文件夹
  6. PolyWorks2020:输入对象 选择单元 剪除单元
  7. c语言奥林匹克大赛真题,全球首发!1-58届国际数学奥林匹克真题及解析大合集,350道必刷、必看、必收藏的巅峰之题与巧解妙解......
  8. 百度高德地图poi数据获取-下载
  9. Centos 7.3 安装Perl
  10. 禁止mac压缩文件、U盘传输到Linux、Windows下出现.DS_Store等隐藏垃圾文件