Guest User

Untitled

a guest
Jul 17th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. // Formats MM/DD/YYYY to YYYYMMDD without providing for single digit MM or DD variants
  2. function formatDateStrict(dateString){
  3. if(typeof dateString === 'string'){
  4. const components = dateString.match(/^(\d{2})\/(\d{2})\/(\d{4})$/);
  5.  
  6. if(components === null){
  7. throw new Error('Expected date string in format MM/DD/YYYY');
  8. }
  9. else {
  10. // Creating extra variables for no real reason apart from readability
  11. const
  12. month = components[1],
  13. day = components[2],
  14. year = components[3];
  15.  
  16. return `${year}${month}${day}`;
  17. }
  18. }
  19. else {
  20. throw new TypeError('A date string is expected');
  21. }
  22. }
Add Comment
Please, Sign In to add comment