skip to Main Content

I have the following array of dates. How can I format each date in this array to ‘mm/dd/yyyy’?

I am attempting something like this which isn’t formatting correctly.
const formattedDates = this.displayEffectiveDate.map(date => new Date(date));

[ "2024-02-29T00:00:00", "2024-03-31T00:00:00" ]

5

Answers


  1. You can use toLocaleDateString() to get the output for your advantage.

    Refer the code below:

    const displayEffectiveDate = [ "2024-02-29T00:00:00", "2024-03-31T00:00:00" ];
    
    const formattedDates = displayEffectiveDate.map(date => {
        const formattedDate = new Date(date).toLocaleDateString('en-US', {
            month: '2-digit',
            day: '2-digit',
            year: 'numeric'
        });
        return formattedDate;
    });
    
    console.log(JSON.stringify(formattedDates));
    Login or Signup to reply.
  2. You can use the Date object, then use method for this like this:

    function dateToMDY(date) {
        var d = date.getDate();
        var m = date.getMonth() + 1; //Month from 0 to 11
        var y = date.getFullYear();
        return (m<=9 ? '0' + m : m) + '/' + (d <= 9 ? '0' + d : d) + "/" + y;
    }
    
    var dates = [ "2024-02-29T00:00:00", "2024-03-31T00:00:00" ];
    console.log(dates.map(d => dateToMDY(new Date(d))));
    Login or Signup to reply.
  3. It seems to me that this is just a matter of string manipulation for which you don’t need to create Date objects:

    const dates = [ "2024-02-29T00:00:00", "2024-03-31T00:00:00" ];
    const result = dates.map(s => s.replace(/(....)-(..)-(..).*/, "$2/$3/$1"));
    
    console.log(result);
    Login or Signup to reply.
  4. Try this

    let array = [ '2024-02-29T00:00:00', '2024-03-31T00:00:00' ]
    
    for(let i = 0; i<array.length; i++){
    
         const a = array[i].split('-', 3)
    
         const day = a[2].split('T', 1)[0]
         const month =  a[1]
         const year = a[0]
    
         const mmddyyyy = `${month}-${day}-${year}`
    
         array[i] = mmddyyyy
    
    }
    
    console.log(array)
    Login or Signup to reply.
  5. If your displayEffectiveDate looks like this:

    this.displayEffectiveDate = ["2024-02-29T00:00:00", "2024-03-31T00:00:00"];

    You could create a new Date object from the ISO 8601 date string.

    d.getMonth() + 1 to get the month

    d.getDate() for the day

    d.getFullYear() for the year

    String(...).padStart(2, '0') to ensure the month and day are always two digits

    It should look like this:

    this.displayEffectiveDate = ["2024-02-29T00:00:00", "2024-03-31T00:00:00"];
    
    const formattedDates = this.displayEffectiveDate.map(date => {
      const d = new Date(date);
      const month = String(d.getMonth() + 1).padStart(2, '0'); // Months are 0-indexed, so we add 1
      const day = String(d.getDate()).padStart(2, '0');
      const year = d.getFullYear();
      return `${month}/${day}/${year}`;
    });
    

    The result would be [ '02/29/2024', '03/31/2024' ]

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search