Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #1894

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Develop #1894

wants to merge 2 commits into from

Conversation

AlexandeeerG
Copy link

Format Date Advanced
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 to YY just use 2 last digit (1997 -> 97).
When converting from YY to YYYY use 20YY if YY < 30 and 19YY 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'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants