在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:Pikaday/Pikaday开源软件地址:https://github.com/Pikaday/Pikaday开源编程语言:JavaScript 78.0%开源软件介绍:PikadayA refreshing JavaScript Datepicker
Production ready? Since version 1.0.0 Pikaday is stable and used in production. If you do however find bugs or have feature requests please submit them to the GitHub issue tracker. Also see the changelog InstallationYou can install Pikaday as an NPM package: npm install pikaday Or link directly to the CDN: <script src="https://cdn.jsdelivr.net/npm/pikaday/pikaday.js"></script> StylesYou will also need to include Pikaday CSS file. This step depends on how Pikaday was installed. Either import from NPM: @import './node_modules/pikaday/css/pikaday.css'; Or link to the CDN: <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/pikaday/css/pikaday.css"> UsagePikaday can be bound to an input field: <input type="text" id="datepicker"> Add the JavaScript to the end of your document: <script src="pikaday.js"></script>
<script>
var picker = new Pikaday({ field: document.getElementById('datepicker') });
</script> If you're using jQuery make sure to pass only the first element: var picker = new Pikaday({ field: $('#datepicker')[0] }); If the Pikaday instance is not bound to a field you can append the element anywhere: var field = document.getElementById('datepicker');
var picker = new Pikaday({
onSelect: function(date) {
field.value = picker.toString();
}
});
field.parentNode.insertBefore(picker.el, field.nextSibling); FormattingBy default, dates are formatted and parsed using standard JavaScript Date object.
If Moment.js is available in scope, it will be used to format and parse input values. You can pass an additional <input type="text" id="datepicker" value="9 Oct 2014">
<script src="moment.js"></script>
<script src="pikaday.js"></script>
<script>
var picker = new Pikaday({
field: document.getElementById('datepicker'),
format: 'D MMM YYYY',
onSelect: function() {
console.log(this.getMoment().format('Do MMMM YYYY'));
}
});
</script> For more advanced and flexible formatting you can pass your own
You should return a string from it. Be careful, though. If the formatted string that you return cannot be correctly parsed by the
var picker = new Pikaday({
field: document.getElementById('datepicker'),
format: 'D/M/YYYY',
toString(date, format) {
// you should do formatting based on the passed format,
// but we will just return 'D/M/YYYY' for simplicity
const day = date.getDate();
const month = date.getMonth() + 1;
const year = date.getFullYear();
return `${day}/${month}/${year}`;
},
parse(dateString, format) {
// dateString is the result of `toString` method
const parts = dateString.split('/');
const day = parseInt(parts[0], 10);
const month = parseInt(parts[1], 10) - 1;
const year = parseInt(parts[2], 10);
return new Date(year, month, day);
}
}); ConfigurationAs the examples demonstrate above Pikaday has many useful options:
StylingIf the
Note that the DOM element at any time will typically have 2 CSS-classes (eg. jQuery PluginThe normal version of Pikaday does not require jQuery, however there is a jQuery plugin if that floats your boat (see <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="pikaday.js"></script>
<script src="plugins/pikaday.jquery.js"></script>
<script>
// 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);
</script> AMD supportIf you use a modular script loader, 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 supportIf 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:
Ruby on RailsIf you're using Ruby on Rails, make sure to check out the Pikaday gem. MethodsYou can control the date picker after creation: var picker = new Pikaday({ field: document.getElementById('datepicker') }); Get and set date
Returns the selected date in a string format. If Moment.js exists (recommended) then Pikaday can return any format that Moment understands.
You can also provide your own If neither
Returns a basic JavaScript
Set the current selection. This will be restricted within the bounds of
Returns a Moment.js object for the selected date (Moment must be loaded before Pikaday).
Set the current selection with a Moment.js object (see Clear and reset date
Will clear and reset the input where picker is bound to. Change current view
Change the current view to see a specific date. This example will jump to February 2014 (month is a zero-based index).
Shortcut for
Change the current view by month (0: January, 1: Februrary, etc).
Go to the next or previous month (this will change year if necessary).
Change the year being viewed.
Update the minimum/earliest date that can be selected.
Update the maximum/latest date that can be selected.
Update the range start date. For using two Pikaday instances to select a date range.
Update the range end date. For using two Pikaday instances to select a date range. Show and hide datepicker
Returns
Make the picker visible.
Recalculate and change the position of the picker.
Hide the picker making it invisible.
Hide the picker and remove all event listeners — no going back! InternationalizationThe default 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 ExtensionsTimepickerPikaday is a pure datepicker. It will not support picking a time of day. However, there have been efforts to add time support to Pikaday. See #1 and #18. These reside in their own fork. You can use the work @owenmead did most recently at owenmead/Pikaday A more simple time selection approach done by @xeeali at xeeali/Pikaday is based on version 1.2.0. Also @stas has a fork stas/Pikaday, but is now quite old Browser Compatibility
Authors
Thanks to @shoogledesigns for the name. Copyright © 2014 David Bushell | BSD & MIT license |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论