Azeem Ansari
Azeem Ansari

Reputation: 47

How can I get the current time using timezone offset using moment.js?

I am using https://openweathermap.org/current api and i want to get current date and time of the target city, I am getting timezone (shift in seconds from UTC) value from api, so how can i get the current time using this timezone offset value.

Lets suppose I have received this below timezone offset value from api

const timezone = -14400

Upvotes: 1

Views: 3067

Answers (2)

Azeem Ansari
Azeem Ansari

Reputation: 47

const timezone = -14400 //needs to be converted in minutes 
const timezoneInMinutes = timezone / 60;
const currTime = moment().utcOffset(timezoneInMinutes).format("h:mm A");
// 10:10 PM

Upvotes: 2

Drew Reese
Drew Reese

Reputation: 203482

const dt = 1593674356; // unix timestamp in seconds
const timezone = 3600; // zone in seconds

// moment.unix - Unix Timestamp (seconds)
const dateTime = moment.unix(dt).utc().add(timezone, 's');

console.log(dateTime);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.27.0/moment.min.js"></script>

Upvotes: 0

Related Questions