--- title: useFormatDateTime description: "`useFormatDateTime` is a custom hook for formatting date time." links: - source: https://github.com/yamada-ui/yamada-ui/tree/main/packages/react/src/components/format - storybook: https://yamada-ui.github.io/yamada-ui?path=/story/hooks-useformatdatetime--basic --- # useFormatDateTime `useFormatDateTime` is a custom hook for formatting date time. ```tsx const [value] = useState(() => new Date()) const formattedValue = useFormatDateTime(value) return {formattedValue} ``` ## Usage ```tsx import { useFormatDateTime } from "@yamada-ui/react" ``` ```tsx import { useFormatDateTime } from "@/components/ui" ``` ```tsx import { useFormatDateTime } from "@workspaces/ui" ``` ```tsx const [value] = useState(() => new Date()) const formattedValue = useFormatDateTime(value) ``` It formats date time according to the specified locale and options. The hook returns the formatted value directly. :::note `useFormatDateTime` internally uses [Intl.DateTimeFormat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat). ::: ### Changing the Locale To change the locale, set a value for [locale](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat#locales). ```tsx const [value] = useState(() => new Date()) const enValue = useFormatDateTime(value, { locale: "en-US" }) const jaValue = useFormatDateTime(value, { locale: "ja-JP" }) const deValue = useFormatDateTime(value, { locale: "de-DE" }) const frValue = useFormatDateTime(value, { locale: "fr-FR" }) const zhValue = useFormatDateTime(value, { locale: "zh-CN" }) return ( en-US {enValue} ja-JP {jaValue} de-DE {deValue} fr-FR {frValue} zh-CN {zhValue} ) ``` ### Set the Locale for the Entire Application If you want to set the locale for the entire application, set the `locale` for the `UIProvider`. ```tsx import { UIProvider } from "@yamada-ui/react" const App = () => { return ( ) } ``` ### Converting to Year To convert to year, set a value for [year](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat#year). ```tsx const [value] = useState(() => new Date()) const formattedValue = useFormatDateTime(value, { year: "numeric" }) return {formattedValue} ``` ### Converting to Month To convert to month, set a value for [month](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat#month). ```tsx const [value] = useState(() => new Date()) const formattedValue = useFormatDateTime(value, { month: "long" }) return {formattedValue} ``` ### Converting to Day To convert to day, set a value for [day](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat#day). ```tsx const [value] = useState(() => new Date()) const formattedValue = useFormatDateTime(value, { day: "2-digit" }) return {formattedValue} ``` ### Converting to Weekday To convert to weekday, set a value for [weekday](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat#weekday). ```tsx const [value] = useState(() => new Date()) const formattedValue = useFormatDateTime(value, { weekday: "long" }) return {formattedValue} ```