date.mjs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Composables
  2. import { useLocale } from "../locale.mjs"; // Utilities
  3. import { inject, reactive, watch } from 'vue';
  4. import { mergeDeep } from "../../util/index.mjs"; // Types
  5. // Adapters
  6. import { VuetifyDateAdapter } from "./adapters/vuetify.mjs";
  7. /** Supports module augmentation to specify date adapter types */
  8. export let DateModule;
  9. export const DateOptionsSymbol = Symbol.for('vuetify:date-options');
  10. export const DateAdapterSymbol = Symbol.for('vuetify:date-adapter');
  11. export function createDate(options, locale) {
  12. const _options = mergeDeep({
  13. adapter: VuetifyDateAdapter,
  14. locale: {
  15. af: 'af-ZA',
  16. // ar: '', # not the same value for all variants
  17. bg: 'bg-BG',
  18. ca: 'ca-ES',
  19. ckb: '',
  20. cs: 'cs-CZ',
  21. de: 'de-DE',
  22. el: 'el-GR',
  23. en: 'en-US',
  24. // es: '', # not the same value for all variants
  25. et: 'et-EE',
  26. fa: 'fa-IR',
  27. fi: 'fi-FI',
  28. // fr: '', #not the same value for all variants
  29. hr: 'hr-HR',
  30. hu: 'hu-HU',
  31. he: 'he-IL',
  32. id: 'id-ID',
  33. it: 'it-IT',
  34. ja: 'ja-JP',
  35. ko: 'ko-KR',
  36. lv: 'lv-LV',
  37. lt: 'lt-LT',
  38. nl: 'nl-NL',
  39. no: 'no-NO',
  40. pl: 'pl-PL',
  41. pt: 'pt-PT',
  42. ro: 'ro-RO',
  43. ru: 'ru-RU',
  44. sk: 'sk-SK',
  45. sl: 'sl-SI',
  46. srCyrl: 'sr-SP',
  47. srLatn: 'sr-SP',
  48. sv: 'sv-SE',
  49. th: 'th-TH',
  50. tr: 'tr-TR',
  51. az: 'az-AZ',
  52. uk: 'uk-UA',
  53. vi: 'vi-VN',
  54. zhHans: 'zh-CN',
  55. zhHant: 'zh-TW'
  56. }
  57. }, options);
  58. return {
  59. options: _options,
  60. instance: createInstance(_options, locale)
  61. };
  62. }
  63. function createInstance(options, locale) {
  64. const instance = reactive(typeof options.adapter === 'function'
  65. // eslint-disable-next-line new-cap
  66. ? new options.adapter({
  67. locale: options.locale[locale.current.value] ?? locale.current.value,
  68. formats: options.formats
  69. }) : options.adapter);
  70. watch(locale.current, value => {
  71. instance.locale = options.locale[value] ?? value ?? instance.locale;
  72. });
  73. return instance;
  74. }
  75. export function useDate() {
  76. const options = inject(DateOptionsSymbol);
  77. if (!options) throw new Error('[Vuetify] Could not find injected date options');
  78. const locale = useLocale();
  79. return createInstance(options, locale);
  80. }
  81. // https://stackoverflow.com/questions/274861/how-do-i-calculate-the-week-number-given-a-date/275024#275024
  82. export function getWeek(adapter, value) {
  83. const date = adapter.toJsDate(value);
  84. let year = date.getFullYear();
  85. let d1w1 = new Date(year, 0, 1);
  86. if (date < d1w1) {
  87. year = year - 1;
  88. d1w1 = new Date(year, 0, 1);
  89. } else {
  90. const tv = new Date(year + 1, 0, 1);
  91. if (date >= tv) {
  92. year = year + 1;
  93. d1w1 = tv;
  94. }
  95. }
  96. const diffTime = Math.abs(date.getTime() - d1w1.getTime());
  97. const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
  98. return Math.floor(diffDays / 7) + 1;
  99. }
  100. //# sourceMappingURL=date.mjs.map