qweeee
qweeee

Reputation: 63

Convert hour UTC time to local time moment.js

I wanted to convert "1" being UTC time to local time like this:

const selectedHour = "1"; const result = moment(selectedHour, "hh").local().format('hh');

But I get "1" itself;

Need your help

Upvotes: 0

Views: 216

Answers (1)

alpakyol
alpakyol

Reputation: 2459

You should treat 1 as UTC. In order to do this, moment has a constructor which treats input as UTC date.

Be careful about formats. h represents 12 hour time without leading zero, hh with leading zero. HH is for 24 hour time. Check https://momentjs.com/docs/#/parsing/string-format/

const selectedHour = "1";

const result = moment.utc(selectedHour, "h").local().format('hh');

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

Upvotes: 2

Related Questions