Skip to content

Instantly share code, notes, and snippets.

@hmetgundogdu
Created April 21, 2022 08:59
Show Gist options
  • Select an option

  • Save hmetgundogdu/c101074025bf3654c3bff958616bc2b7 to your computer and use it in GitHub Desktop.

Select an option

Save hmetgundogdu/c101074025bf3654c3bff958616bc2b7 to your computer and use it in GitHub Desktop.
export type SupportedLanguages =
'tr-TR' |
'ka-GE' |
'en-US' |
'ar-EG' |
'ru-RU';
;
function useDateLocalizers() {
// Hooks
const { i18n } = useTranslation();
// Memorize
const currentLanguage = i18n.language as SupportedLanguages;
// Methods
const formatDateTime = (date: Date) => {
switch (currentLanguage) {
case 'tr-TR':
return format(date, 'dd.MM.yyyy HH:mm:ss');
case 'ka-GE':
return format(date, 'dd.MM.yyyy HH:mm:ss');
case 'en-US':
return format(date, 'MM/dd/yyyy hh:mm:ss a');
case 'ar-EG':
return format(date, 'dd.MM.yyyy HH:mm:ss');
case 'ru-RU':
return format(date, 'dd.MM.yyyy HH:mm:ss');
default:
return format(date, 'dd.MM.yyyy HH:mm:ss');
}
}
const formatDate = (date: Date) => {
switch (currentLanguage) {
case 'tr-TR':
return format(date, 'dd.MM.yyyy');
case 'ka-GE':
return format(date, 'dd.MM.yyyy');
case 'en-US':
return format(date, 'MM/dd/yyyy');
case 'ar-EG':
return format(date, 'dd.MM.yyyy');
case 'ru-RU':
return format(date, 'dd.MM.yyyy');
default:
return format(date, 'dd.MM.yyyy');
}
}
const formatTime = (date: Date) => {
switch (currentLanguage) {
case 'tr-TR':
return format(date, 'HH:mm:ss');
case 'ka-GE':
return format(date, 'HH:mm:ss');
case 'en-US':
return format(date, 'hh:mm:ss a');
case 'ar-EG':
return format(date, 'HH:mm:ss');
case 'ru-RU':
return format(date, 'HH:mm:ss');
default:
return format(date, 'HH:mm:ss');
}
}
return {
formatDateTime,
formatDate,
formatTime
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment