Advertisement
funcelot

Untitled

Aug 11th, 2023
1,132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function formatDateCustom(date, format) {
  2.   const year = date.getFullYear().toString();
  3.   const month = (date.getMonth() + 1).toString().padStart(2, '0');
  4.   const day = date.getDate().toString().padStart(2, '0');
  5.  
  6.   return format
  7.     .replace('YYY', year)
  8.     .replace('MM', month)
  9.     .replace('DD', day);
  10. }
  11.  
  12. const inputDate = new Date('2023-08-11');
  13. const customFormat = 'YYYMMDD';
  14. const formattedDate = formatDateCustom(inputDate, customFormat);
  15. console.log(formattedDate); // Output: "20230811"
  16.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement