UNPKG

@engine2/efa-utils

Version:

Library for EFA Apps utility functions

58 lines (47 loc) 2.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = formatDate; var _format = _interopRequireDefault(require("date-fns/format")); var _parse = _interopRequireDefault(require("date-fns/parse")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var formats = { isoDate: 'yyyy-MM-dd', czechDate: 'dd.MM.yyyy', dayDate: 'd. M.', shortDate: 'ddMMMyy', shortTime: 'HHmm', mediumTime: 'HH:mm', fullTime: 'HH:mm:ss', fullDatetime: 'yyyy-MM-dd HH:mm:ss' /** * A date and time formater. * Important: function use date-fns (v2.0.0-alpha.25) library. * @param {Date | string} datetime as a instance of Date object or a string. * @param {string} format - Name of the predefined format, see below, or date-fns format. In the case you provide Date as the first argument this will be required output format. If you provide string as the first argument, this has to be datetime format of that argument. * @param {string} format - Name of the predefined format, see below, or date-fns format. This argument is required only if the first argument is string, in that case this argument specify a required output format. * @returns {string} Returns the datetime in required formatted string. * Names for common datetime formats: * isoDate: 'yyyy-MM-dd' * czechDate: 'dd.MM.yyyy' * dayDate: 'd. M.' * shortDate: 'ddMMMyy' * shortTime: 'HHmm' * mediumTime: 'HH:mm' * fullTime: 'HH:mm:ss' * fullDatetime: 'yyyy-MM-dd HH:mm:ss' */ }; function formatDate(date, formatIn, formatOut) { if (date === null || date === undefined) return null; if (formatIn === undefined) throw 'You have to specify another arguments!'; if (typeof date === 'string') { if (formatOut === undefined) throw 'Your date is string, you have to specify both input format and output format!'; date = (0, _parse.default)(date, formats[formatIn] || formatIn, new Date()); } if (formatOut === undefined) formatOut = formatIn; var formatted = (0, _format.default)(date, formats[formatOut] || formatOut); // short format is transformed to uppercase return formatOut === 'shortDate' ? formatted.toUpperCase() : formatted; } module.exports = exports.default;