UNPKG

d2l-intl

Version:

D2L internationalization APIs for number, date and time formatting and parsing.

36 lines (27 loc) 894 B
import prePadByZero from '../util/pre-pad-by-zero.js'; import processPattern from '../util/process-pattern.js'; export default function formatTime(date, localeData, options) { options.format = options.format || 'short'; options.timezone = options.timezone || ''; var format = localeData.date.formats.timeFormats[options.format]; if (format === undefined) { format = options.format; } var hour = date.getHours(); var hour12 = hour % 12; if (hour12 === 0) { hour12 = 12; } var calendar = localeData.date.calendar; var replacements = { 'HH': prePadByZero(date.getHours(), 2), 'H': date.getHours().toString(), 'hh': prePadByZero(hour12, 2), 'h': hour12, 'mm': prePadByZero(date.getMinutes(), 2), 'tt': (hour > 11) ? calendar.dayPeriods.pm : calendar.dayPeriods.am, 'ZZZ': options.timezone }; var value = processPattern(format, replacements); return value; }