diff --git a/format.js b/format.js new file mode 100644 index 0000000..f88ba4e --- /dev/null +++ b/format.js @@ -0,0 +1,56 @@ +import { useTranslation } from "react-i18next"; + +export function useNumberFormat() { + const { i18n } = useTranslation(); + + return (number, digits=2, currency=null) => { + if (currency) { + if((Math.round(number * (10**digits))/(10**digits)) === 0) + return "---"; + return new Intl.NumberFormat(i18n.language, { + style: "currency", + currency: currency, + minimumFractionDigits: digits, + maximumFractionDigits: digits, + }).format(number); + } else { + return new Intl.NumberFormat(i18n.language, { + minimumFractionDigits: digits, + maximumFractionDigits: digits, + }).format(number); + } + } +} + +export function useDateFormat() { + const { i18n } = useTranslation(); + + return (date) => { + if(typeof date === "undefined") return "---"; + if(typeof date === "string") date = new Date(date); + + return new Intl.DateTimeFormat(i18n.language, { + year: "numeric", + month: "short", + day: "numeric", + }).format(date); + } +} +export function useDateTimeFormat() { + const { i18n } = useTranslation(); + + return (date) => { + if(typeof date === "undefined") return "---"; + if(typeof date === "string") date = new Date(date); + + return new Intl.DateTimeFormat(i18n.language, { + year: "numeric", + month: "short", + day: "numeric", + + hour: "numeric", + minute: "numeric", + second: "numeric", + }).format(date); + } +} \ No newline at end of file