From 85bdff3224c392d171d9b41c7d84a1e1a5d80329 Mon Sep 17 00:00:00 2001 From: Vladyslav Date: Tue, 22 Oct 2024 16:31:25 +0200 Subject: [PATCH] formatDate function v1.1 --- src/formatDate.js | 56 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 48 insertions(+), 8 deletions(-) diff --git a/src/formatDate.js b/src/formatDate.js index 769e2766..57d0e6ac 100644 --- a/src/formatDate.js +++ b/src/formatDate.js @@ -1,14 +1,54 @@ 'use strict'; -/** - * @param {string} date - * @param {string[]} fromFormat - * @param {string[]} toFormat - * - * @returns {string} - */ function formatDate(date, fromFormat, toFormat) { - // write code here + const prevSplitter = fromFormat[3]; + const newSplitter = toFormat[3]; + const rawDate = date.split(prevSplitter); + const newRawDate = []; + + function setTheDate(dateType, nextDateType = dateType) { + newRawDate[toFormat.indexOf(nextDateType)] = + rawDate[fromFormat.indexOf(dateType)]; + } + + setTheDate('DD'); + setTheDate('MM'); + + if (fromFormat.includes('YYYY') && toFormat.includes('YYYY')) { + setTheDate('YYYY'); + } + + if (fromFormat.includes('YYYY') && toFormat.includes('YY')) { + setTheDate('YYYY', 'YY'); + + newRawDate[toFormat.indexOf('YY')] = newRawDate[ + toFormat.indexOf('YY') + ].slice(2, 4); + } + + if (fromFormat.includes('YY') && toFormat.includes('YYYY')) { + if (rawDate[fromFormat.indexOf('YY')] < 30) { + newRawDate[toFormat.indexOf('YYYY')] = + `20${rawDate[fromFormat.indexOf('YY')]}`; + } + + if (rawDate[fromFormat.indexOf('YY')] >= 30) { + newRawDate[toFormat.indexOf('YYYY')] = + `19${rawDate[fromFormat.indexOf('YY')]}`; + } + } + + const newDate = newRawDate.join(newSplitter); + + return newDate; } +formatDate('2020-02-18', ['YYYY', 'MM', 'DD', '-'], ['YYYY', 'MM', 'DD', '.']); + +formatDate('18-02-2020', ['DD', 'MM', 'YYYY', '-'], ['DD', 'MM', 'YY', '/']); + +formatDate('20/02/18', ['YY', 'MM', 'DD', '/'], ['YYYY', 'MM', 'DD', '.']); + +formatDate('97/02/18', ['YY', 'MM', 'DD', '/'], ['DD', 'MM', 'YYYY', '.']); + module.exports = formatDate;