time()
time(input, options?): Time
time(input, zone?): Time
time(hour, minute?, second?): Time時刻文字列、整数の時・分・秒、フィールドオブジェクト、またはタイムスタンプから Time を作成します。入力が欠けている場合は現在時刻にフォールバックせず、TypeError をスローします。
import { datetime, time } from 'native-dates';
const morning = time('9am');
const closing = time('5:30pm');
const iso = time('17:30:00');
const fromNumbers = time(9, 30);
const fromFields = time({ hour: 17, minute: 30, millisecond: 250 });
const fromInstant = time('2026-11-16T17:30:00Z', 'America/New_York');
const fromMoment = time(datetime('2026-11-16T17:30:00Z'), 'Europe/Berlin');
const fromTemporal = time(Temporal.PlainTime.from('08:15'));
closing.toString(); // '17:30:00'
fromInstant.toString(); // '12:30:00'
fromMoment.toString(); // '18:30:00'引数
時刻文字列、フィールドオブジェクト、既存の時刻またはタイムスタンプ、あるいは0〜23の24時間制 hour。
タイムスタンプや loose 文字列を読み取る際のタイムゾーンまたはオプションオブジェクト、あるいは0〜59の minute。
4件のオプション
瞬間を表す文字列、DateTime、または Date から時刻を取り出す際に使うタイムゾーン。
返される Time とそこから派生するすべての値に引き継がれる BCP 47 ロケールタグ。
false@native-dates/plugins/loose を使用し、非定型の日時文字列を読み取り、読み取れない入力には null を返す設定。
loose モードで日付と時刻を含む文字列を解析する際、アクティブなロケールの数値日付順を上書きする設定。
数値で渡す際の0〜59の整数 second。秒未満の桁は文字列またはフィールドオブジェクトで指定。
戻り値
1日の中の時刻を表す Time。loose モードで input を読み取れない場合は null。
loose プラグインを読み込むと、loose モードでは各言語の時間帯表記(午前・午後など)、ドット区切りを採用するロケールの時刻表記、および非定型のタイムスタンプ文字列を解析できます。読み取れない入力に対しては null を返します。
import { time } from 'native-dates';
import '@native-dates/plugins/loose';
time('오후 2:30', { loose: true, locale: 'ko' })?.toString(); // '14:30:00'
time('14.30', { loose: true, locale: 'fi' })?.toString(); // '14:30:00'
time('Mon, 16 Nov 2026 17:30:00 GMT', {
zone: 'America/New_York',
loose: true,
})?.toString(); // '12:30:00'
time('whenever', { loose: true }); // null