Translate GMT date string into PST date string and format output as ‘mm/dd/yyyy – hh:mi’ [closed]

I have an array like this with elements

var getDaysArray = [
    "2021-10-28T08:00:00",
    "2021-10-27T08:00:00",
    "2021-10-31T03:00:00"
]

I need to get the following result MM/DD/YYYY – HH:MM PM Pacific Time

I was able to replace “-“ with “/” using replace

for(var i = 0; i < getDaysArray.length; i++) {
  document.write(getDaysArray[i].replace("-", "/"));
}

But I need to get the following result

10/28/2021 – 19:25

Answer

Parse your input strings into dates and transform to desired locale string with appropriate set of options:

const datesArray = [
          "2021-10-28T08:00:00",
          "2021-10-27T08:00:00",
          "2021-10-31T03:00:00"
      ],
      
      translateDateStr = dateStr => {
        const [yyyy, mm, dd, hh, mi, ss] = dateStr.split(/[-T:]/)
        const date = new Date(Date.UTC(yyyy, mm-1, dd, hh, mi, ss))
        
        return date
          .toLocaleString(
            'en-US',
            {
              timeZone: 'America/Los_Angeles',
              hour12: false
            }
          )
          .slice(0,-3)
          .replace(', ', ' - ')
      },
      
      formattedDates = datesArray.map(translateDateStr)
      
console.log(formattedDates)
.as-console-wrapper{min-height:100%}