UNPKG

4.19 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = formatISO9075;
7
8var _index = _interopRequireDefault(require("../toDate/index.js"));
9
10var _index2 = _interopRequireDefault(require("../isValid/index.js"));
11
12var _index3 = _interopRequireDefault(require("../_lib/addLeadingZeros/index.js"));
13
14function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
16/**
17 * @name formatISO9075
18 * @category Common Helpers
19 * @summary Format the date according to the ISO 9075 standard (https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_get-format).
20 *
21 * @description
22 * Return the formatted date string in ISO 9075 format. Options may be passed to control the parts and notations of the date.
23 *
24 * @param {Date|Number} date - the original date
25 * @param {Object} [options] - an object with options.
26 * @param {'extended'|'basic'} [options.format='extended'] - if 'basic', hide delimiters between date and time values.
27 * @param {'complete'|'date'|'time'} [options.representation='complete'] - format date, time, or both.
28 * @returns {String} the formatted date string
29 * @throws {TypeError} 1 argument required
30 * @throws {RangeError} `date` must not be Invalid Date
31 * @throws {RangeError} `options.format` must be 'extended' or 'basic'
32 * @throws {RangeError} `options.represenation` must be 'date', 'time' or 'complete'
33 *
34 * @example
35 * // Represent 18 September 2019 in ISO 9075 format:
36 * const result = formatISO9075(new Date(2019, 8, 18, 19, 0, 52))
37 * //=> '2019-09-18 19:00:52'
38 *
39 * @example
40 * // Represent 18 September 2019 in ISO 9075, short format:
41 * const result = formatISO9075(new Date(2019, 8, 18, 19, 0, 52), { format: 'basic' })
42 * //=> '20190918 190052'
43 *
44 * @example
45 * // Represent 18 September 2019 in ISO 9075 format, date only:
46 * const result = formatISO9075(new Date(2019, 8, 18, 19, 0, 52), { representation: 'date' })
47 * //=> '2019-09-18'
48 *
49 * @example
50 * // Represent 18 September 2019 in ISO 9075 format, time only:
51 * const result = formatISO9075(new Date(2019, 8, 18, 19, 0, 52), { representation: 'time' })
52 * //=> '19:00:52'
53 */
54function formatISO9075(dirtyDate, dirtyOptions) {
55 if (arguments.length < 1) {
56 throw new TypeError("1 argument required, but only ".concat(arguments.length, " present"));
57 }
58
59 var originalDate = (0, _index.default)(dirtyDate);
60
61 if (!(0, _index2.default)(originalDate)) {
62 throw new RangeError('Invalid time value');
63 }
64
65 var options = dirtyOptions || {};
66 var format = options.format == null ? 'extended' : String(options.format);
67 var representation = options.representation == null ? 'complete' : String(options.representation);
68
69 if (format !== 'extended' && format !== 'basic') {
70 throw new RangeError("format must be 'extended' or 'basic'");
71 }
72
73 if (representation !== 'date' && representation !== 'time' && representation !== 'complete') {
74 throw new RangeError("representation must be 'date', 'time', or 'complete'");
75 }
76
77 var result = '';
78 var dateDelimiter = format === 'extended' ? '-' : '';
79 var timeDelimiter = format === 'extended' ? ':' : ''; // Representation is either 'date' or 'complete'
80
81 if (representation !== 'time') {
82 var day = (0, _index3.default)(originalDate.getDate(), 2);
83 var month = (0, _index3.default)(originalDate.getMonth() + 1, 2);
84 var year = (0, _index3.default)(originalDate.getFullYear(), 4); // yyyyMMdd or yyyy-MM-dd.
85
86 result = "".concat(year).concat(dateDelimiter).concat(month).concat(dateDelimiter).concat(day);
87 } // Representation is either 'time' or 'complete'
88
89
90 if (representation !== 'date') {
91 var hour = (0, _index3.default)(originalDate.getHours(), 2);
92 var minute = (0, _index3.default)(originalDate.getMinutes(), 2);
93 var second = (0, _index3.default)(originalDate.getSeconds(), 2); // If there's also date, separate it with time with a space
94
95 var separator = result === '' ? '' : ' '; // HHmmss or HH:mm:ss.
96
97 result = "".concat(result).concat(separator).concat(hour).concat(timeDelimiter).concat(minute).concat(timeDelimiter).concat(second);
98 }
99
100 return result;
101}
102
103module.exports = exports.default;
\No newline at end of file