强大 多语言日期/时间选择器 Tempus Dominus

  • 源码大小:1.58MB
  • 所需积分:1积分
  • 源码编号:19JP-3063
  • 浏览次数:1216次
  • 最后更新:2023年05月01日
  • 所属栏目:时间和时钟
我要下载
加入收藏
本站默认解压密码:19jp.com 或 19jp_com

简介

Tempus Dominus Bootstrap 4插件的书面版本。

Tempus Dominus是一款功能强大、完全可定制、轻量级的Vanilla JavaScript和jQuery日期/时间选择器。它是对本地HTML5日期和时间输入类型的零依赖性替换

特征:

  • 多种语言。
  • 自动语言检测。
  • 5种视图模式:“时钟”|“日历”|“月”|“年”|“几十年”。
  • 允许启用/禁用日期。
  • 允许选择日期范围。
  • 允许禁用/启用日期和时间。
  • 允许禁用一周中的几天。
  • 允许选择多个日期。
  • 智能定位(需要popperjs库)。
  • 删除了jQuery和Moment.js的依赖项。

目录:

  • 初始化
  • 选项
  • 方法
  • 事件

如何使用它:

1.要开始,请在文档中加载所需的popperjs库、Font Awesome 5和Tempus Dominus文件。

  1. <link rel="stylesheet" href="/path/to/dist/css/tempus-dominus.css" />
  2. <link rel="stylesheet" href="/path/to/[email protected]/all.min.css" />
  3. <script src="/path/to/[email protected]/popper.min.js"></script>
  4. <script src="/path/to/dist/js/tempus-dominus.js"></script>

2.您仍然可以将其用作jQuery插件。

  1. <script src="/path/to/jquery.min.js"></script>
  2. <script src="/path/to/dist/js/jQuery-provider.min.js"></script>

3.将日期选择器附加到输入字段。

  1. <input id="example" type="text" />
  1. // Vanilla JS
  2. new tempusDominus.TempusDominus(document.getElementById('example'), {
  3. // options here
  4. });
  5.  
  6. // jQuery
  7. $('#example').tempusDominus({
  8. // options here
  9. });

4.启用触发按钮以切换日期选择器。

  1. <div
  2. class="input-group"
  3. id="example"
  4. data-td-target-input="nearest"
  5. data-td-target-toggle="nearest"
  6. >
  7. <input
  8. id="exampleInput"
  9. type="text"
  10. data-td-target="#example"
  11. />
  12. <span
  13. class="input-group-text"
  14. data-td-target="#example"
  15. data-td-toggle="datetimepicker"
  16. >
  17. <span class="fas fa-calendar"></span>
  18. </span>
  19. </div>
  1. // Vanilla JS
  2. new tempusDominus.TempusDominus(document.getElementById('example'), {
  3. // options here
  4. });
  5.  
  6. // jQuery
  7. $('#example').tempusDominus({
  8. // options here
  9. });

5.您还可以将日期选择器附加到文档中的任何元素。

  1. <span
  2. id="example"
  3. >
  4. <i class="fa-solid fa-calendar"></i>
  5. </span>
  1. // Vanilla JS
  2. new tempusDominus.TempusDominus(document.getElementById('example'), {
  3. // options here
  4. });
  5.  
  6. // jQuery
  7. $('#example').tempusDominus({
  8. // options here
  9. });

6.设置日期选择器的语言。当地可用:

  • de
  • en
  • es
  • it
  • nl
  • ro
  • ru
  1. new tempusDominus.TempusDominus(document.getElementById('example'), {
  2. localization: {
  3. dayViewHeaderFormat: { month: 'long', year: '2-digit' },
  4. locale: 'es',
  5. startOfTheWeek: 0
  6. },
  7. });

7.组合两个日期选择器以创建一个日期范围选择器。

  1. <!-- Date Picker 1 -->
  2. <label for="linkedPickers1Input" class="form-label">From</label>
  3. <div
  4. class="input-group log-event"
  5. id="linkedPickers1"
  6. data-td-target-input="nearest"
  7. data-td-target-toggle="nearest"
  8. >
  9. <input
  10. id="linkedPickers1Input"
  11. type="text"
  12. class="form-control"
  13. data-td-target="#linkedPickers1"
  14. />
  15. <span
  16. class="input-group-text"
  17. data-td-target="#linkedPickers1"
  18. data-td-toggle="datetimepicker"
  19. >
  20. <span class="fa-solid fa-calendar"></span>
  21. </span>
  22. </div>
  23.  
  24. <!-- Date Picker 2 -->
  25. <label for="linkedPickers2Input" class="form-label">To</label>
  26. <div
  27. class="input-group log-event"
  28. id="linkedPickers2"
  29. data-td-target-input="nearest"
  30. data-td-target-toggle="nearest"
  31. >
  32. <input
  33. id="linkedPickers2Input"
  34. type="text"
  35. class="form-control"
  36. data-td-target="#linkedPickers2"
  37. />
  38. <span
  39. class="input-group-text"
  40. data-td-target="#linkedPickers2"
  41. data-td-toggle="datetimepicker"
  42. >
  43. <span class="fa-solid fa-calendar"></span>
  44. </span>
  45. </div>
  1. const linkedPicker1Element = document.getElementById('linkedPickers1');
  2. const linked1 = new tempusDominus.TempusDominus(linkedPicker1Element);
  3. const linked2 = new tempusDominus.TempusDominus(document.getElementById('linkedPickers2'), {
  4. useCurrent: false
  5. });
  6. // using event listeners
  7. linkedPicker1Element.addEventListener(tempusDominus.Namespace.events.change, (e) => {
  8. linked2.updateOptions({
  9. restrictions: {
  10. minDate: e.detail.date
  11. }
  12. });
  13. });
  14. // using subscribe method
  15. const subscription = linked2.subscribe(tempusDominus.Namespace.events.change, (e) => {
  16. linked1.updateOptions({
  17. restrictions: {
  18. maxDate: e.date
  19. }
  20. });
  21. });

8.所有默认选项:

  1. const linkedPicker1Element = document.getElementById('linkedPickers1');
  2. new tempusDominus.TempusDominus(document.getElementById('example'), {
  3.  
  4. // enable/disable dates & times
  5. restrictions: {
  6. minDate: undefined,
  7. maxDate: undefined,
  8. disabledDates: [],
  9. enabledDates: [],
  10. daysOfWeekDisabled: [],
  11. disabledTimeIntervals: [],
  12. disabledHours: [],
  13. enabledHours: []
  14. },
  15.  
  16. // display options
  17. display: {
  18. icons: {
  19. // "icons" or "sprites"
  20. type: 'icons',
  21. time: 'fa-solid fa-clock',
  22. date: 'fa-solid fa-calendar',
  23. up: 'fa-solid fa-arrow-up',
  24. down: 'fa-solid fa-arrow-down',
  25. previous: 'fa-solid fa-chevron-left',
  26. next: 'fa-solid fa-chevron-right',
  27. today: 'fa-solid fa-calendar-check',
  28. clear: 'fa-solid fa-trash',
  29. close: 'fa-solid fa-xmark'
  30. },
  31. // display the date and time pickers side by side
  32. sideBySide: false,
  33. // display an additional column with the calendar week for that week
  34. calendarWeeks: false,
  35. // 'clock' | 'calendar' | 'months' | 'years' | 'decades'
  36. viewMode: 'calendar',
  37. // 'top' | 'bottom'
  38. toolbarPlacement: 'bottom',
  39. keepOpen: false,
  40. // enable/disable buttons
  41. buttons: {
  42. today: false,
  43. clear: false,
  44. close: false
  45. },
  46. // enable/disable components
  47. components: {
  48. calendar: true,
  49. date: true,
  50. month: true,
  51. year: true,
  52. decades: true,
  53. clock: true,
  54. hours: true,
  55. minutes: true,
  56. seconds: false,
  57. useTwentyfourHour: false
  58. },
  59. // inline mode
  60. inline: false
  61. },
  62.  
  63. // control how much the minutes are changed by
  64. stepping: 1,
  65.  
  66. // use the current date/time
  67. useCurrent: true,
  68.  
  69. defaultDate: undefined,
  70.  
  71. localization: {
  72. today: 'Go to today',
  73. clear: 'Clear selection',
  74. close: 'Close the picker',
  75. selectMonth: 'Select Month',
  76. previousMonth: 'Previous Month',
  77. nextMonth: 'Next Month',
  78. selectYear: 'Select Year',
  79. previousYear: 'Previous Year',
  80. nextYear: 'Next Year',
  81. selectDecade: 'Select Decade',
  82. previousDecade: 'Previous Decade',
  83. nextDecade: 'Next Decade',
  84. previousCentury: 'Previous Century',
  85. nextCentury: 'Next Century',
  86. pickHour: 'Pick Hour',
  87. incrementHour: 'Increment Hour',
  88. decrementHour: 'Decrement Hour',
  89. pickMinute: 'Pick Minute',
  90. incrementMinute: 'Increment Minute',
  91. decrementMinute: 'Decrement Minute',
  92. pickSecond: 'Pick Second',
  93. incrementSecond: 'Increment Second',
  94. decrementSecond: 'Decrement Second',
  95. toggleMeridiem: 'Toggle Meridiem',
  96. selectTime: 'Select Time',
  97. selectDate: 'Select Date',
  98. dayViewHeaderFormat: { month: 'long', year: '2-digit' },
  99. locale: 'default',
  100. // 0 = Sunday, 6 = Saturday
  101. startOfTheWeek: 0
  102. },
  103.  
  104. keepInvalid: false,
  105.  
  106. debug: false,
  107.  
  108. // show the date picker on focus
  109. allowInputToggle: false,
  110.  
  111. viewDate: new DateTime(),
  112.  
  113. multipleDates: false,
  114.  
  115. multipleDatesSeparator: '; ',
  116.  
  117. promptTimeOnDateChange: false,
  118.  
  119. promptTimeOnDateChangeTransitionDelay: 200,
  120.  
  121. // provide developers a place to store extra information about the picker
  122. meta: {},
  123.  
  124. // change the target container
  125. container: undefined,
  126.  
  127. // enable date range selection
  128. dateRange: false,
  129.  
  130. // custom separator for multiple dates
  131. multipleDatesSeparator: '; ',
  132.  
  133. // placemenet
  134. placement: 'bottom',
  135. });

9.API方法:

  1. const myPicker = new tempusDominus.TempusDominus(document.getElementById('example'), {
  2. // ...
  3. });
  4.  
  5. // return the current date
  6. myPicker.viewDate
  7.  
  8. // return a DateTime object
  9. myPicker.dates
  10.  
  11. // return the picked date
  12. myPicker.dates.picked
  13.  
  14. // return the last picked date
  15. myPicker.dates.lastPicked
  16.  
  17. // return the last picked index
  18. myPicker.dates.lastPickedIndex
  19.  
  20. // add a DateTime
  21. myPicker.dates.add(DateTime);
  22.  
  23. // set the date index
  24. myPicker.dates.setValue(value: DateTime, index?: number);
  25.  
  26. // convert a DateTime object to a string
  27. myPicker.dates.formatInput(value: DateTime);
  28.  
  29. // convert a string into a DateTime object
  30. myPicker.dates.setFromInput(value: any, index?: number);
  31.  
  32. // return true if the target date is part of the selected dates array
  33. myPicker.dates.isPicked(DateTime, Unit?);
  34.  
  35. // return the index at which target date is in the array
  36. myPicker.dates.pickedIndex(DateTime, Unit?);
  37.  
  38. // clear all picked dates
  39. myPicker.dates.clear();
  40. // OR
  41. myPicker.clear();
  42.  
  43. // toggle the date picker
  44. myPicker.toggle();
  45.  
  46. // show the date picker
  47. myPicker.show();
  48.  
  49. // hide the date picker
  50. myPicker.hide();
  51.  
  52. // add/remove CSS classes
  53. myPicker.paint(Unit | 'decade', DateTime, string[], HTMLElement);
  54.  
  55. // enable the date input
  56. myPicker.enable();
  57.  
  58. // disable the date input
  59. myPicker.disable();
  60.  
  61. // toggle the date input
  62. myPicker.enable();
  63.  
  64. // destroy the date picker
  65. myPicker.dispose();
  66.  
  67. // update options
  68. myPicker.updateOptions(object, boolean?);

10.事件:

  1. const myPicker = new tempusDominus.TempusDominus(document.getElementById('example'), {
  2. // ...
  3. });
  4.  
  5. const subscription = picker.subscribe(tempusdominus.Namespace.events.change, (e) => {
  6. // on change
  7. });
  8.  
  9. const subscription = picker.subscribe(tempusdominus.Namespace.events.show, (e) => {
  10. // on show
  11. });
  12.  
  13. const subscription = picker.subscribe(tempusdominus.Namespace.events.hide, (e) => {
  14. // on hide
  15. });
  16.  
  17. const subscription = picker.subscribe(tempusdominus.Namespace.events.update, (e) => {
  18. // on update
  19. });
  20.  
  21. const subscription = picker.subscribe(tempusdominus.Namespace.events.error, (e) => {
  22. // on error
  23. });

更新日志:

版本6.7.7(2023-04-23)

  • 放置选项
  • 导出默认的美国语言环境
  • 当userCurrent为false时,时钟现在将显示——而不是
  • 错误修复程序

版本6.4.4(2023-03-22)

  • 错误修复

版本6.4.3(2023-03-17)

  • 修复了日期格式的问题
  • 固定格式转义括号

版本6.4.1(2023-03-15)

  • 将自定义日期格式迁移到主代码
  • 添加了日期范围功能
  • 固定多个日期时添加的前导分隔符

版本6.2.10(2022-12-24)

  • 错误修复

版本6.2.9(2022-12-16)

  • 错误修复

版本6.2.8(2022-12-15)

  • 预提交挂钩。Linter和Beatler现在在每次提交之前都会运行。
  • 本地语言和插件现在都包含了打字员。
  • 错误修复。

版本6.2.7(2022-11-05)

  • 错误修复程序

版本6.2.6(2022-10-29)

  • 修复了禁用/启用日期的性能问题。这也应该修复下个月/上个月的选择
  • 修复了跨选项同步的视图日期和未正确更新的问题

版本6.2.5(2022-10-25)

  • 增加了波兰本地化
  • 已更新区域设置以包含格式
  • 导出(重新导出?)选项界面

版本6.2.4(2022-09-29)

  • 错误修复程序

版本6.2.3(2022-09-28)

  • 固定popper参考

版本6.1.2(2022-09-23)

  • 现在可以通过

版本6.0.1(2022-08-31)

  • 大量改进和错误修复

版本6.0.0(2022-08-16)

  • 大量改进和错误修复

 

预览截图