Reputation: 177
I have only seen converting date from UTC to local, but what about converting just the hour from UTC?
For example:
moment.utc(hour, "HH").local().format("h")
this doesn't actually work.
Upvotes: 1
Views: 611
Reputation: 241475
Works for me:
var hour = 4;
moment.utc(hour, "HH").local().format("h") //=> "8"
Screenshot from Chrome debug console:
Do keep in mind the following:
h
is an hour on a 12-hour clock. You probably want to use H
or HH
. Otherwise you can't distinguish between AM and PM, unless you also use a
or A
in the output format.
Without providing a date, Moment is using the current day. Due to daylight saving time and changes in standard time, not all time zones will have the same offset from UTC for every date. Thus, the output of this code will change depending on when you run it.
Here's a snippet (including the AM/PM) you can run for yourself if you like:
var hour = 4;
var result = moment.utc(hour, "HH").local().format("h A");
document.getElementById('output').innerHTML = result;
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
Local Time: <span id="output"></span>
Upvotes: 1