1

I'm trying to use .format() to convert the inputted time into 'ddd, MMM DD, hh:mma' format, but it returns it in the wrong timezone compared to the original intended timezone (America_NewYork).

const someday = moment('2023-01-27T12:21:08.088Z').format('ddd, MMM DD, hh:mma');

const out = document.getElementById('output');
const someday = moment('2023-01-27T12:21:08.088Z').format('ddd, MMM DD, hh:mma');
out.innerText = someday;
<script src="https://momentjs.com/downloads/moment.js"></script>
<p>Expected outcome: Fri, Jan 27, 12:21pm </p>
<p>Actual outcome: <span id="output"></span></p>
Blue Moon
  • 79
  • 1
  • 9

1 Answers1

1

All you have to do is get the UTC time and then use the local() method to convert that UTC date-time to your local time. Here is a sample code.

Just doing moment.utc().format('YYYY-MM-DD HH:mm'), will give you the UTC time. You can also set UTC offset like moment(testDateUtc).utcOffset(10 * 60); //set timezone offset in minutes.

const local = moment(moment.utc()).local().format('YYYY-MM-DD HH:mm');
console.log(local);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
Giovanni Esposito
  • 10,696
  • 1
  • 14
  • 30