diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-09-20 13:18:24 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-09-20 13:18:24 +0000 |
commit | 0653e08efd039a5905f3fa4f6e9cef9f5d2f799c (patch) | |
tree | 4dcc884cf6d81db44adae4aa99f8ec1233a41f55 /app/assets/javascripts/lib/utils/datetime/date_format_utility.js | |
parent | 744144d28e3e7fddc117924fef88de5d9674fe4c (diff) | |
download | gitlab-ce-0653e08efd039a5905f3fa4f6e9cef9f5d2f799c.tar.gz |
Add latest changes from gitlab-org/gitlab@14-3-stable-eev14.3.0-rc42
Diffstat (limited to 'app/assets/javascripts/lib/utils/datetime/date_format_utility.js')
-rw-r--r-- | app/assets/javascripts/lib/utils/datetime/date_format_utility.js | 105 |
1 files changed, 104 insertions, 1 deletions
diff --git a/app/assets/javascripts/lib/utils/datetime/date_format_utility.js b/app/assets/javascripts/lib/utils/datetime/date_format_utility.js index 246f290a90a..0a35efb0ac8 100644 --- a/app/assets/javascripts/lib/utils/datetime/date_format_utility.js +++ b/app/assets/javascripts/lib/utils/datetime/date_format_utility.js @@ -1,5 +1,5 @@ import dateFormat from 'dateformat'; -import { isString, mapValues, reduce } from 'lodash'; +import { isString, mapValues, reduce, isDate } from 'lodash'; import { s__, n__, __ } from '../../../locale'; /** @@ -258,3 +258,106 @@ export const parseSeconds = ( return periodCount; }); }; + +/** + * Pads given items with zeros to reach a length of 2 characters. + * + * @param {...any} args Items to be padded. + * @returns {Array<String>} Padded items. + */ +export const padWithZeros = (...args) => args.map((arg) => `${arg}`.padStart(2, '0')); + +/** + * This removes the timezone from an ISO date string. + * This can be useful when populating date/time fields along with a distinct timezone selector, in + * which case we'd want to ignore the timezone's offset when populating the date and time. + * + * Examples: + * stripTimezoneFromISODate('2021-08-16T00:00:00.000-02:00') => '2021-08-16T00:00:00.000' + * stripTimezoneFromISODate('2021-08-16T00:00:00.000Z') => '2021-08-16T00:00:00.000' + * + * @param {String} date The ISO date string representation. + * @returns {String} The ISO date string without the timezone. + */ +export const stripTimezoneFromISODate = (date) => { + if (Number.isNaN(Date.parse(date))) { + return null; + } + return date.replace(/(Z|[+-]\d{2}:\d{2})$/, ''); +}; + +/** + * Extracts the year, month and day from a Date instance and returns them in an object. + * For example: + * dateToYearMonthDate(new Date('2021-08-16')) => { year: '2021', month: '08', day: '16' } + * + * @param {Date} date The date to be parsed + * @returns {Object} An object containing the extracted year, month and day. + */ +export const dateToYearMonthDate = (date) => { + if (!isDate(date)) { + // eslint-disable-next-line @gitlab/require-i18n-strings + throw new Error('Argument should be a Date instance'); + } + const [month, day] = padWithZeros(date.getMonth() + 1, date.getDate()); + return { + year: `${date.getFullYear()}`, + month, + day, + }; +}; + +/** + * Extracts the hours and minutes from a string representing a time. + * For example: + * timeToHoursMinutes('12:46') => { hours: '12', minutes: '46' } + * + * @param {String} time The time to be parsed in the form HH:MM. + * @returns {Object} An object containing the hours and minutes. + */ +export const timeToHoursMinutes = (time = '') => { + if (!time || !time.match(/\d{1,2}:\d{1,2}/)) { + // eslint-disable-next-line @gitlab/require-i18n-strings + throw new Error('Invalid time provided'); + } + const [hours, minutes] = padWithZeros(...time.split(':')); + return { hours, minutes }; +}; + +/** + * This combines a date and a time and returns the computed Date's ISO string representation. + * + * @param {Date} date Date object representing the base date. + * @param {String} time String representing the time to be used, in the form HH:MM. + * @param {String} offset An optional Date-compatible offset. + * @returns {String} The combined Date's ISO string representation. + */ +export const dateAndTimeToISOString = (date, time, offset = '') => { + const { year, month, day } = dateToYearMonthDate(date); + const { hours, minutes } = timeToHoursMinutes(time); + const dateString = `${year}-${month}-${day}T${hours}:${minutes}:00.000${offset || 'Z'}`; + if (Number.isNaN(Date.parse(dateString))) { + // eslint-disable-next-line @gitlab/require-i18n-strings + throw new Error('Could not initialize date'); + } + return dateString; +}; + +/** + * Converts a Date instance to time input-compatible value consisting in a 2-digits hours and + * minutes, separated by a semi-colon, in the 24-hours format. + * + * @param {Date} date Date to be converted + * @returns {String} time input-compatible string in the form HH:MM. + */ +export const dateToTimeInputValue = (date) => { + if (!isDate(date)) { + // eslint-disable-next-line @gitlab/require-i18n-strings + throw new Error('Argument should be a Date instance'); + } + return date.toLocaleTimeString([], { + hour: '2-digit', + minute: '2-digit', + hour12: false, + }); +}; |