Read the guideline before start
Time flies, standards change. Let's get rid of the routine of changing the date format.
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
toYY
just use2
last digit (1997
->97
). - When converting from
YY
toYYYY
use20YY
ifYY < 30
and19YY
otherwise.
Examples:
formatDate(
'2020-02-18',
['YYYY', 'MM', 'DD', '-'],
['YYYY', 'MM', 'DD', '.'],
); // '2020.02.18'
formatDate(
'2020-02-18',
['YYYY', 'MM', 'DD', '-'],
['DD', 'MM', 'YYYY', '.'],
); // '18.02.2020'
formatDate(
'18-02-2020',
['DD', 'MM', 'YYYY', '-'],
['DD', 'MM', 'YY', '/'],
); // '18/02/20'
formatDate(
'20/02/18',
['YY', 'MM', 'DD', '/'],
['YYYY', 'MM', 'DD', '.'],
); // '2020.02.18'
formatDate(
'97/02/18',
['YY', 'MM', 'DD', '/'],
['DD', 'MM', 'YYYY', '.'],
); // '18.02.1997'