From bccde19ffef2aeb225b55e9718d08ac49b9de4e0 Mon Sep 17 00:00:00 2001 From: Polianskyi Dmytro Date: Tue, 7 Jan 2025 18:48:36 +0200 Subject: [PATCH] add solution --- package-lock.json | 9 +++++---- package.json | 2 +- src/formatDate.js | 47 +++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 51 insertions(+), 7 deletions(-) diff --git a/package-lock.json b/package-lock.json index 5794158b..77e8d3a6 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": "^1.9.12", "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": "1.9.12", + "resolved": "https://registry.npmjs.org/@mate-academy/scripts/-/scripts-1.9.12.tgz", + "integrity": "sha512-/OcmxMa34lYLFlGx7Ig926W1U1qjrnXbjFJ2TzUcDaLmED+A5se652NcWwGOidXRuMAOYLPU2jNYBEkKyXrFJA==", "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 0df4c1ec..f7c6e873 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": "^1.9.12", "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 769e2766..3c692f1c 100644 --- a/src/formatDate.js +++ b/src/formatDate.js @@ -8,7 +8,50 @@ * @returns {string} */ function formatDate(date, fromFormat, toFormat) { - // write code here -} + const fromSpliter = fromFormat[fromFormat.length - 1]; + const toSpliter = toFormat[toFormat.length - 1]; + let longYear = 0; + let shortYear = 0; + let year = ''; + let month = 0; + let day = 0; + const dateArray = []; + + for (let i = 0; i < fromFormat.length - 1; i++) { + if (fromFormat[i] === 'YYYY') { + longYear = date.split(fromSpliter)[i]; + } else if (fromFormat[i] === 'YY') { + shortYear = date.split(fromSpliter)[i]; + } else if (fromFormat[i] === 'MM') { + month = date.split(fromSpliter)[i]; + } else { + day = date.split(fromSpliter)[i]; + } + } + for (let i = 0; i < toFormat.length - 1; i++) { + if (toFormat[i] === 'YYYY' && longYear !== 0) { + dateArray.push(longYear); + } else if (toFormat[i] === 'YY' && shortYear !== 0) { + dateArray.push(shortYear); + } else if (toFormat[i] === 'YY' && longYear !== 0) { + year += longYear; + year = year.split('').splice(2).join(''); + dateArray.push(Number(year)); + } else if (toFormat[i] === 'YYYY' && shortYear !== 0) { + if (shortYear < 30) { + year += '20' + shortYear; + } else { + year += '19' + shortYear; + } + dateArray.push(Number(year)); + } else if (toFormat[i] === 'MM') { + dateArray.push(month); + } else { + dateArray.push(day); + } + } + + return dateArray.join(toSpliter); +} module.exports = formatDate;