Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/test.yml-template
Original file line number Diff line number Diff line change
@@ -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
9 changes: 5 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
60 changes: 60 additions & 0 deletions src/formatDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Loading