formatDateTime
Standalone function for formatting dates and times according to locale conventions
Overview
The standalone formatDateTime function formats dates and times according to locale-specific conventions without needing a GT instance.
It offers the same functionality as the GT class method but can be used independently.
import { formatDateTime } from 'generaltranslation';
const formatted = formatDateTime(new Date(), {
locales: 'de-DE',
dateStyle: 'medium',
timeStyle: 'short'
});
// Returns: "26.09.2025, 17:33"Reference
Parameters
| Name | Type | Description |
|---|---|---|
date | Date | The date object to format |
options | DateTimeFormatOptions & { locales?: string | string[] } | Formatting configuration with optional locales |
DateTimeFormatOptions
| Name | Type | Description |
|---|---|---|
locales? | string | string[] | Locales for formatting (defaults to system locale) |
dateStyle? | 'full' | 'long' | 'medium' | 'short' | Overall date formatting style |
timeStyle? | 'full' | 'long' | 'medium' | 'short' | Overall time formatting style |
weekday? | 'long' | 'short' | 'narrow' | Weekday representation |
year? | 'numeric' | '2-digit' | Year representation |
month? | 'numeric' | '2-digit' | 'long' | 'short' | 'narrow' | Month representation |
day? | 'numeric' | '2-digit' | Day representation |
hour? | 'numeric' | '2-digit' | Hour representation |
minute? | 'numeric' | '2-digit' | Minute representation |
second? | 'numeric' | '2-digit' | Second representation |
timeZone? | string | IANA time zone identifier |
hour12? | boolean | Whether to use 12‑hour time format |
Returns
string - The formatted date and time according to locale conventions.
Example
Basic usage
import { formatDateTime } from 'generaltranslation';
// Basic formatting with explicit locale
console.log(formatDateTime(date, { locales: 'en-US' }));
// Output: "14/03/2024"
// German formatting
console.log(formatDateTime(date, { locales: 'de-DE' }));
// Output: "14.3.2024"
// Multiple locale fallbacks
console.log(formatDateTime(date, {
locales: ['ja-JP', 'en-US']
}));
// Output: "2024/3/14" (Japanese format)Date and time styles
const date = new Date('2024-03-14T14:30:45Z');
// Full date style
console.log(formatDateTime(date, {
locales: 'en-US',
dateStyle: 'full'
}));
// Output: "Thursday, March 14, 2024"
// Long date with short time
console.log(formatDateTime(date, {
locales: 'fr-FR',
dateStyle: 'long',
timeStyle: 'short'
}));
// Output: "14 mars 2024 à 07:30"
// Short format across locales
const locales = ['en-US', 'de-DE', 'ja-JP'];
locales.forEach(locale => {
console.log(`${locale}: ${formatDateTime(date, {
locales: locale,
dateStyle: 'short',
timeStyle: 'short'
})}`);
});
// Output:
// en-US: 3/14/24, 7:30 AM
// de-DE: 14.03.24, 07:30
// ja-JP: 2024/03/14 7:30Time zone handling
const date = new Date('2024-03-14T14:30:45Z');
// Format for different time zones
const timeZones = [
'America/New_York',
'Europe/London',
'Asia/Tokyo'
];
timeZones.forEach(timeZone => {
const formatted = formatDateTime(date, {
locales: 'en-US',
timeZone,
dateStyle: 'medium',
timeStyle: 'medium'
});
console.log(`${timeZone}: ${formatted}`);
});
// Output varies based on daylight saving timeNotes
- The
localesparameter is optional and defaults to the system locale if not provided - Uses the same underlying
Intl.DateTimeFormatas the GT class method - Results are cached internally for performance with repeated locale/options combinations
- All standard
Intl.DateTimeFormatoptions are supported - Time zones are handled correctly when specified
- Different locales have different default date/time formats and 12-hour vs 24-hour preferences
Next steps
- Check the
Intl.DateTimeFormatdocumentation for more options - See
formatNumfor standalone number formatting - See
formatMessagefor standalone message formatting - See the GT class
formatDateTimefor instance-based usage - See
formatRelativeTimefor relative time formatting
How is this guide?