From 0354ec99a576e81efbbefad77e18a5d28b92f5b5 Mon Sep 17 00:00:00 2001 From: Bohuslav Stanishevskyy Date: Wed, 1 Jul 2026 13:47:45 +0300 Subject: [PATCH] feat: complete date formatter --- .github/workflows/test.yml-template | 23 +++++++++++ package-lock.json | 9 +++-- package.json | 2 +- src/formatDate.js | 60 +++++++++++++++++++++++++++++ 4 files changed, 89 insertions(+), 5 deletions(-) create mode 100644 .github/workflows/test.yml-template diff --git a/.github/workflows/test.yml-template b/.github/workflows/test.yml-template new file mode 100644 index 000000000..bb13dfc45 --- /dev/null +++ b/.github/workflows/test.yml-template @@ -0,0 +1,23 @@ +name: Test + +on: + pull_request: + branches: [ master ] + +jobs: + build: + + runs-on: ubuntu-latest + + strategy: + matrix: + node-version: [20.x] + + steps: + - uses: actions/checkout@v2 + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v1 + with: + node-version: ${{ matrix.node-version }} + - run: npm install + - run: npm test diff --git a/package-lock.json b/package-lock.json index 5794158b4..83e6357ac 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,7 @@ "license": "GPL-3.0", "devDependencies": { "@mate-academy/eslint-config": "latest", - "@mate-academy/scripts": "^1.8.6", + "@mate-academy/scripts": "^2.1.3", "eslint": "^8.57.0", "eslint-plugin-jest": "^28.6.0", "eslint-plugin-node": "^11.1.0", @@ -1467,10 +1467,11 @@ } }, "node_modules/@mate-academy/scripts": { - "version": "1.8.6", - "resolved": "https://registry.npmjs.org/@mate-academy/scripts/-/scripts-1.8.6.tgz", - "integrity": "sha512-b4om/whj4G9emyi84ORE3FRZzCRwRIesr8tJHXa8EvJdOaAPDpzcJ8A0sFfMsWH9NUOVmOwkBtOXDu5eZZ00Ig==", + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/@mate-academy/scripts/-/scripts-2.1.3.tgz", + "integrity": "sha512-a07wHTj/1QUK2Aac5zHad+sGw4rIvcNl5lJmJpAD7OxeSbnCdyI6RXUHwXhjF5MaVo9YHrJ0xVahyERS2IIyBQ==", "dev": true, + "license": "MIT", "dependencies": { "@octokit/rest": "^17.11.2", "@types/get-port": "^4.2.0", diff --git a/package.json b/package.json index 0df4c1ec0..33f22fea8 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "license": "GPL-3.0", "devDependencies": { "@mate-academy/eslint-config": "latest", - "@mate-academy/scripts": "^1.8.6", + "@mate-academy/scripts": "^2.1.3", "eslint": "^8.57.0", "eslint-plugin-jest": "^28.6.0", "eslint-plugin-node": "^11.1.0", diff --git a/src/formatDate.js b/src/formatDate.js index 769e27661..b7ff0991e 100644 --- a/src/formatDate.js +++ b/src/formatDate.js @@ -9,6 +9,66 @@ */ function formatDate(date, fromFormat, toFormat) { // write code here + const oldSeparator = fromFormat[fromFormat.length - 1]; + const newSeparator = toFormat[toFormat.length - 1]; + let oldYearPosition = fromFormat.indexOf('YYYY'); + let newYearPosition = toFormat.indexOf('YYYY'); + const oldMonthPosition = fromFormat.indexOf('MM'); + const newMonthPosition = toFormat.indexOf('MM'); + const oldDayPosition = fromFormat.indexOf('DD'); + const newDayPosition = toFormat.indexOf('DD'); + const dateParts = date.split(oldSeparator); + let year = dateParts[oldYearPosition]; + const month = dateParts[oldMonthPosition]; + const day = dateParts[oldDayPosition]; + const newDateArray = []; + + if (oldYearPosition === -1) { + oldYearPosition = fromFormat.indexOf('YY'); + year = dateParts[oldYearPosition]; + + if (newYearPosition !== -1) { + if (year < 30) { + year = '20' + year; + } else { + year = '19' + year; + } + } + } + + if (newYearPosition === -1) { + newYearPosition = toFormat.indexOf('YY'); + + if (year.length === 4) { + year = year.slice(2); + } + } + + newDateArray[newYearPosition] = year; + newDateArray[newMonthPosition] = month; + newDateArray[newDayPosition] = day; + + const newDate = newDateArray.join(newSeparator); + + return newDate; } module.exports = formatDate; + +// Create a `formatDate` function that accepts the `date` string, +// the old `fromFormat` array and the new `toFormat` array. +// Function returns given date in new format. + +// The function can change a separator, reorder the date parts +// of convert a year from `4` digits to `2` digits and back. + +// - When converting from `YYYY` to `YY` just +// use `2` last digit (`1997` -> `97`). +// - When converting from `YY` to `YYYY` use `20YY` +// if `YY < 30` and `19YY` otherwise. + +// formatDate( +// '20/02/18', +// ['YY', 'MM', 'DD', '/'], +// ['YYYY', 'MM', 'DD', '.'], +// ); // '2020.02.18'