UNPKG

date-fns

Version:

Modern JavaScript date utility library

90 lines (87 loc) 3.43 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _index = _interopRequireDefault(require("../../addLeadingZeros/index.js")); /* * | | Unit | | Unit | * |-----|--------------------------------|-----|--------------------------------| * | a | AM, PM | A* | | * | d | Day of month | D | | * | h | Hour [1-12] | H | Hour [0-23] | * | m | Minute | M | Month | * | s | Second | S | Fraction of second | * | y | Year (abs) | Y | | * * Letters marked by * are not implemented but reserved by Unicode standard. */ var formatters = { // Year y: function y(date, token) { // From http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_tokens // | Year | y | yy | yyy | yyyy | yyyyy | // |----------|-------|----|-------|-------|-------| // | AD 1 | 1 | 01 | 001 | 0001 | 00001 | // | AD 12 | 12 | 12 | 012 | 0012 | 00012 | // | AD 123 | 123 | 23 | 123 | 0123 | 00123 | // | AD 1234 | 1234 | 34 | 1234 | 1234 | 01234 | // | AD 12345 | 12345 | 45 | 12345 | 12345 | 12345 | var signedYear = date.getUTCFullYear(); // Returns 1 for 1 BC (which is year 0 in JavaScript) var year = signedYear > 0 ? signedYear : 1 - signedYear; return (0, _index.default)(token === 'yy' ? year % 100 : year, token.length); }, // Month M: function M(date, token) { var month = date.getUTCMonth(); return token === 'M' ? String(month + 1) : (0, _index.default)(month + 1, 2); }, // Day of the month d: function d(date, token) { return (0, _index.default)(date.getUTCDate(), token.length); }, // AM or PM a: function a(date, token) { var dayPeriodEnumValue = date.getUTCHours() / 12 >= 1 ? 'pm' : 'am'; switch (token) { case 'a': case 'aa': return dayPeriodEnumValue.toUpperCase(); case 'aaa': return dayPeriodEnumValue; case 'aaaaa': return dayPeriodEnumValue[0]; case 'aaaa': default: return dayPeriodEnumValue === 'am' ? 'a.m.' : 'p.m.'; } }, // Hour [1-12] h: function h(date, token) { return (0, _index.default)(date.getUTCHours() % 12 || 12, token.length); }, // Hour [0-23] H: function H(date, token) { return (0, _index.default)(date.getUTCHours(), token.length); }, // Minute m: function m(date, token) { return (0, _index.default)(date.getUTCMinutes(), token.length); }, // Second s: function s(date, token) { return (0, _index.default)(date.getUTCSeconds(), token.length); }, // Fraction of second S: function S(date, token) { var numberOfDigits = token.length; var milliseconds = date.getUTCMilliseconds(); var fractionalSeconds = Math.floor(milliseconds * Math.pow(10, numberOfDigits - 3)); return (0, _index.default)(fractionalSeconds, token.length); } }; var _default = formatters; exports.default = _default; module.exports = exports.default;