# gt-react: General Translation React SDK: DateTime URL: https://generaltranslation.com/en-US/docs/react/api/components/datetime.mdx --- title: DateTime description: API reference for the DateTime component --- {/* AUTO-GENERATED: Do not edit directly. Edit the template in content/docs-templates/ instead. */} ## Overview The `` component renders a formatted date or time string, supporting customization such as formatting options and locale. The date or time is formatted according to the current locale and any optional parameters passed. ```jsx {1738010355} // Output: 1/27/2025 ``` All formatting is handled locally using the [`Intl.DateTimeFormat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat) library. `` can cause **React hydration errors** in server-rendered apps. See [Avoiding hydration errors](#avoiding-hydration-errors) below. ## Reference ### Props ### Description | Prop Name | Description | |-----------|-------------| | `children` | The content to render inside the component. Typically a date or time value. If provided, it takes precedence over the `value` prop. | | `value` | The default value for the date or time. Can be a string, number (timestamp), or Date object. | | `options` | Optional formatting options for the date or time, following the [`Intl.DateTimeFormatOptions` specification](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat). Use this to define styles such as weekday names, time zones, and more. | | `locales` | Optional locales to specify the formatting locale. If not provided, the user's locale is used. Read more about specifying locales [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#locales_argument).| ### Returns `JSX.Element` containing the formatted date or time as a string. --- ## Examples ### Basic usage The `` component can be used to display localized date or time values. ```jsx title="EventDate.jsx" copy import { DateTime } from 'gt-react'; export default function EventDate(event) { return ( {event.date} . // [!code highlight] ); } ``` ### Specifying locales The `` component can be used to display date or time values in a specific locale. ```jsx title="EventDate.jsx" copy import { DateTime } from 'gt-react'; export default function EventDate(event) { return ( {event.date} . // [!code highlight] ); } ``` ### Translating DateTime Say that you want the datetime to be displayed in a sentence that is also being translated. You can wrap the `` component in a `` component. ```jsx title="EventDate.jsx" copy import { T, DateTime } from 'gt-react'; export default function EventDate(event) { return ( The time of the event is {event.date} . // [!code highlight] ); } ``` ### Custom formatting The `` component supports custom formatting options. ```jsx title="EventDate.jsx" copy import { DateTime } from 'gt-react'; export default function EventDate(event) { return ( {event.date} . ); } ``` --- ## Avoiding hydration errors Because `` formats dates locally using `Intl.DateTimeFormat`, it can produce **different output on the server and client**. When React compares the server-rendered HTML to the client render and they don't match, you get a hydration error. This typically happens when: - **No explicit `timeZone` is set.** The server may run in UTC (or another zone), while the user's browser uses their local time zone. A timestamp like `1738010355` could render as `"1/27/2025"` on the server but `"1/28/2025"` on the client if the time zones differ. - **The default locale differs between environments.** If the server's default locale doesn't match the client's, the formatted string may differ (e.g. `"27/01/2025"` vs `"1/27/2025"`). ### How to fix it **Set an explicit `timeZone` in `options`** to ensure consistent output: ```jsx {event.date} ``` **Specify explicit `locales`** to avoid locale mismatches: ```jsx {event.date} ``` By pinning both the locale and time zone, the server and client will always produce the same formatted string. ## Notes * The `` component is a variable component that can be used to format date and time values. * The component uses the [`Intl.DateTimeFormat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat) library for formatting. ## Next steps * For more details and usage examples of the `` component and other variable components like ``, ``, and ``, see the [Using Variable Components](/docs/react/guides/variables) documentation.