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 6974af62276674d83621f6e73f2221716944dba5..51244b4ffd5491279e1a8ef870e0558b460ea6f6 100644 --- a/app/assets/javascripts/lib/utils/datetime/date_format_utility.js +++ b/app/assets/javascripts/lib/utils/datetime/date_format_utility.js @@ -510,3 +510,18 @@ export const formatTimeSpent = (seconds, limitToHours) => { const negative = seconds < 0; return (negative ? '- ' : '') + stringifyTime(parseSeconds(seconds, { limitToHours })); }; + +/** + * Formats a date into ISO 8601 date format (yyyy-mm-dd) + * @param { number } year The full year (e.g., 2023) + * @param { number } monthIndex The zero-based month index (0-11, where 0 = January) + * @param { number } day The day of the month (1-31) + * @returns { string } Date string in ISO 8601 format (yyyy-mm-dd) + * @example formatIso8601Date(2023, 0, 15) // returns '2023-01-15' + */ +export const formatIso8601Date = (year, monthIndex, day) => { + return [year, monthIndex + 1, day] + .map(String) + .map((s) => s.padStart(2, '0')) + .join('-'); +}; diff --git a/ee/app/assets/javascripts/usage_quotas/pipelines/components/app.vue b/ee/app/assets/javascripts/usage_quotas/pipelines/components/app.vue index 238bee24254fbc1a7f5d2a58b9f9d6d986362a39..e9f415ae298cff2c68d177d68d52b65882f7fafc 100644 --- a/ee/app/assets/javascripts/usage_quotas/pipelines/components/app.vue +++ b/ee/app/assets/javascripts/usage_quotas/pipelines/components/app.vue @@ -9,7 +9,7 @@ import { GlSprintf, } from '@gitlab/ui'; import { getSubscriptionPermissionsData } from 'ee/fulfillment/shared_queries/subscription_actions_reason.customer.query.graphql'; -import { getMonthNames } from '~/lib/utils/datetime_utility'; +import { getMonthNames, formatIso8601Date } from '~/lib/utils/datetime_utility'; import { TYPENAME_GROUP } from '~/graphql_shared/constants'; import { convertToGraphQLId } from '~/graphql_shared/utils'; import { captureException } from '~/ci/runner/sentry_utils'; @@ -21,7 +21,7 @@ import getCiMinutesUsageNamespace from '../graphql/queries/ci_minutes.query.grap import getCiMinutesUsageNamespaceProjects from '../graphql/queries/ci_minutes_projects.query.graphql'; import { ERROR_MESSAGE, LABEL_BUY_ADDITIONAL_MINUTES } from '../constants'; import { USAGE_BY_MONTH_HEADER, USAGE_BY_PROJECT_HEADER } from '../../constants'; -import { getUsageDataByYearAsArray, formatIso8601Date } from '../utils'; +import { getUsageDataByYearAsArray } from '../utils'; import LimitedAccessModal from '../../components/limited_access_modal.vue'; import ProjectList from './project_list.vue'; import MinutesUsagePerMonth from './minutes_usage_per_month.vue'; diff --git a/ee/app/assets/javascripts/usage_quotas/pipelines/utils.js b/ee/app/assets/javascripts/usage_quotas/pipelines/utils.js index a2d94da8a0bf2cc6641981fd76fbdf32344623aa..917d537b73e935aa0113020ca393b5901367b0d1 100644 --- a/ee/app/assets/javascripts/usage_quotas/pipelines/utils.js +++ b/ee/app/assets/javascripts/usage_quotas/pipelines/utils.js @@ -36,20 +36,3 @@ export const getUsageDataByYearByMonthAsObject = (ciMinutesUsage) => { return acc; }, {}); }; - -/** - * Formats date to `yyyy-mm-dd` - * @param { number } year full year - * @param { number } monthIndex month index, between 0 and 11 - * @param { number } day day of the month - * @returns { string } formatted date string - * - * NOTE: it might be worth moving this utility to date time utils - * in ~/lib/utils/datetime_utility.js - */ -export const formatIso8601Date = (year, monthIndex, day) => { - return [year, monthIndex + 1, day] - .map(String) - .map((s) => s.padStart(2, '0')) - .join('-'); -}; diff --git a/ee/spec/frontend/usage_quotas/pipelines/utils_spec.js b/ee/spec/frontend/usage_quotas/pipelines/utils_spec.js index 0597831a612e7fc3efbb350ac005ff57a8a73a09..e62c1df3559632e31e706f1eda10f15e50a3d11a 100644 --- a/ee/spec/frontend/usage_quotas/pipelines/utils_spec.js +++ b/ee/spec/frontend/usage_quotas/pipelines/utils_spec.js @@ -1,7 +1,6 @@ import { getUsageDataByYearAsArray, getUsageDataByYearByMonthAsObject, - formatIso8601Date, } from 'ee/usage_quotas/pipelines/utils'; import { mockGetCiMinutesUsageNamespace } from './mock_data'; @@ -105,10 +104,4 @@ describe('Compute minutes Usage Utils', () => { expect(getUsageDataByYearByMonthAsObject(nodes)).toEqual(expectedDataByYearMonth); }); - - describe('formatIso8601Date', () => { - it('creates a ISO-8601 formated date', () => { - expect(formatIso8601Date(2021, 5, 1)).toBe('2021-06-01'); - }); - }); }); diff --git a/spec/frontend/lib/utils/datetime/date_format_utility_spec.js b/spec/frontend/lib/utils/datetime/date_format_utility_spec.js index 146e9d569f6208f323bee2267b57dfc93e0bcd96..0eb50641ed4b5c5d06a1391f3ade22de31a76755 100644 --- a/spec/frontend/lib/utils/datetime/date_format_utility_spec.js +++ b/spec/frontend/lib/utils/datetime/date_format_utility_spec.js @@ -542,4 +542,10 @@ describe('date_format_utility.js', () => { }); }); }); + + describe('formatIso8601Date', () => { + it('creates a ISO-8601 formated date', () => { + expect(utils.formatIso8601Date(2021, 5, 1)).toBe('2021-06-01'); + }); + }); });