Skip to content

Commit

Permalink
guess who wants to see AM/PM in their datepickers
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Sep 24, 2024
1 parent e403d21 commit de5b3e8
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 2 deletions.
2 changes: 2 additions & 0 deletions ui/bits/src/bits.flatpickr.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import flatpickr from 'flatpickr';
import { use24h } from 'common/i18n';

site.load.then(() => {
$('.flatpickr').each(function(this: HTMLInputElement) {
Expand All @@ -11,6 +12,7 @@ site.load.then(() => {
maxDate: new Date(Date.now() + 1000 * 3600 * 24 * 31 * 12),
monthSelectorType: 'static',
disableMobile: true, // https://flatpickr.js.org/mobile-support/ https://github.com/lichess-org/lila/issues/8110
time_24hr: use24h(),
...config,
});
});
Expand Down
2 changes: 2 additions & 0 deletions ui/bits/src/bits.tourForm.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import flatpickr from 'flatpickr';
import { use24h } from 'common/i18n';

site.load.then(() => {
const $variant = $('#form3-variant'),
Expand All @@ -17,6 +18,7 @@ site.load.then(() => {
altFormat: 'Y-m-d h:i K',
monthSelectorType: 'static',
disableMobile: true,
time_24hr: use24h(),
});
});
});
9 changes: 7 additions & 2 deletions ui/common/src/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ export const displayLocale: string = document.documentElement.lang.startsWith('a
? 'ar-ly'
: document.documentElement.lang;;

export const commonDateFormat: (d?: Date|number) => string = new Intl.DateTimeFormat(displayLocale, {
const commonDateFormatter = new Intl.DateTimeFormat(displayLocale, {
year: 'numeric',
month: 'short',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
}).format;
});

export const commonDateFormat: (d?: Date|number) => string = commonDateFormatter.format;

export const timeago: (d: DateLike) => string =
(date: DateLike) => formatAgo((Date.now() - toDate(date).getTime()) / 1000);
Expand All @@ -49,6 +51,9 @@ export const timeago: (d: DateLike) => string =
export const toDate = (input: DateLike): Date =>
input instanceof Date ? input : new Date(isNaN(input as any) ? input : parseInt(input as any));

export const use24h = (): boolean =>
!new Intl.DateTimeFormat(displayLocale).resolvedOptions().hour12;

// format the diff second to *** time ago
export const formatAgo = (seconds: number): string => {
const absSeconds = Math.abs(seconds);
Expand Down
2 changes: 2 additions & 0 deletions ui/swiss/src/view/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import * as boards from './boards';
import podium from './podium';
import playerInfo from './playerInfo';
import flatpickr from 'flatpickr';
import { use24h } from 'common/i18n';
import { once } from 'common/storage';
import { initMiniGames } from 'common/miniBoard';
import { watchers } from 'common/watchers';
Expand Down Expand Up @@ -107,6 +108,7 @@ function nextRound(ctrl: SwissCtrl): VNode | undefined {
onClose() {
(el.parentNode as HTMLFormElement).submit();
},
time_24hr: use24h(),
}),
),
}),
Expand Down

0 comments on commit de5b3e8

Please sign in to comment.