formatTime and parseTime
formatTime(time, { locale }) writes a time in the locale's short time style, such as 9:30 PM in en-US, 21:30 in de and 오후 9:30 in ko, or in its medium style, with seconds, when the time has seconds. parseTime(text, { locale }) reads typed text back into a Temporal.PlainTime, or returns null when the text is not a time. Without locale, both use the runtime's own. Field.TimeInput reads and writes its text with them, and timeOptions writes its labels with formatTime.
On this page
import { Code, DataList, Field, formatTime, Label, parseTime, Stack } from '@tounsoo/todae';
import { useId, useState } from 'react';
// What parseTime reads from the text, and how formatTime writes it back.
export default function TimeRoundTrip({ locale }: { locale: string }) {
const id = useId();
const [text, setText] = useState('2130');
const value = parseTime(text, { locale });
return (
<Stack.Root gap="condensed">
<Field.Group>
<Label.Root htmlFor={id}>Opens at</Label.Root>
<Field.Root id={id}>
<Field.TextInput value={text} onChange={(event) => setText(event.target.value)} />
</Field.Root>
</Field.Group>
<DataList.Root aria-label="Round trip">
<DataList.Item>
<DataList.Term>parseTime</DataList.Term>
<DataList.Details>
<Code.Root>{value === null ? 'null' : value.toString()}</Code.Root>
</DataList.Details>
</DataList.Item>
<DataList.Item>
<DataList.Term>formatTime</DataList.Term>
<DataList.Details>
<Code.Root>{value === null ? 'null' : formatTime(value, { locale })}</Code.Root>
</DataList.Details>
</DataList.Item>
</DataList.Root>
</Stack.Root>
);
}
- parseTime
21:30:00- formatTime
9:30 PM
What parseTime reads
It reads an hour, an hour and minutes, or an hour, minutes and seconds, such as 9, 9:30, 21.30.15 or 0930, with the locale's own word for the part of the day before or after them, such as 9:30 pm in en-US or 오후 9:30 in ko. Letter case doesn't matter, digits may be in any script, and the numbers may be separated by any punctuation, symbols or spaces, or by words the locale writes between them, such as 21 h 30 in fr-CA. Without a word for the part of the day, the hour is 0 to 23.
Temporal
Todae ships no Temporal polyfill. Both use Temporal.PlainTime, so where the browser has no Temporal, the app loads a polyfill (see Native platform).
Accessible composition
Todae handles: Text in the locale you pass, or the runtime's own when you pass none.
You supply: A locale that matches the page's, and the same one for writing and reading. A message near the input when parseTime returns null.
Watch for: On a server with no locale, the text uses the server's locale.
When to use
formatTime
When: Writing a time as text for a locale, such as into a text input of your own.
When not: A time in another style, such as with a time zone → Intl.DateTimeFormat.
parseTime
When: Reading a time someone typed.
When not: Text in a fixed format such as 09:30 → Temporal.PlainTime.from.
Props
| Name | Type | Description |
|---|---|---|
time Required | Temporal.PlainTime | The time to write. |
options.locale | string | The locale, such as ko. The runtime's own when not set. |
| Name | Type | Description |
|---|---|---|
text Required | string | The typed text. |
options.locale | string | The locale, such as ko. The runtime's own when not set. |