how to properly get day of the week (name) with moment.js?

Im using https://openweathermap.org/ api. It provides timezone in seconds. How to properly get day of the week (name) using moment.js?

 const timezoneInMinutes = 7200 / 60;
 const currentDate = moment().utcOffset(timezoneInMinutes).format("YYYY-MM-DD");
 console.log(currentDate) // 2022-05-13
 console.log(moment().day(currentDate).format("dddd")); // "Sunday" ????

>Solution :

You are putting currentDate into day(), you need to put it on moment().

const timezoneInMinutes = 7200 / 60;
const currentDate = moment().utcOffset(timezoneInMinutes).format("YYYY-MM-DD");
console.log(currentDate) // 2022-05-13
console.log(moment(currentDate).format("dddd")); // "Sunday" ????
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.3/moment.min.js"></script>

Leave a Reply