Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Formats MM/DD/YYYY to YYYYMMDD without providing for single digit MM or DD variants
- function formatDateStrict(dateString){
- if(typeof dateString === 'string'){
- const components = dateString.match(/^(\d{2})\/(\d{2})\/(\d{4})$/);
- if(components === null){
- throw new Error('Expected date string in format MM/DD/YYYY');
- }
- else {
- // Creating extra variables for no real reason apart from readability
- const
- month = components[1],
- day = components[2],
- year = components[3];
- return `${year}${month}${day}`;
- }
- }
- else {
- throw new TypeError('A date string is expected');
- }
- }
Add Comment
Please, Sign In to add comment