How to convert one time format to another in javascript using moment.tz?

Viewed 46

I have tried various methods but I'm unable to convert date like 1640638800 to 2022-11-27 16:00:00 America/New_York timezone format in javascript using moment.tz.

My current code was like

import * as moment from 'moment';
var date= new Date(0);
date.setUTCSeconds(1640638800);
moment.tz(date.getFullYear()+"-"+date.getMonth()+"-"+date.getDay() + " 10:00:00", "America/New_York");
1 Answers
import moment from 'moment-timezone'

// Unix Timestamp (milliseconds)
const ts = 1640638800 * 1000 

// Parse as timezoned moment
const m = moment.tz(ts, "America/New_York")

// Format moment to string
const formatted = m.format('YYYY-MM-DD HH:mm:ss')

console.log(formatted) // '2021-12-27 16:00:00'
Related