webslash
webslash

Reputation: 53

Moment.js How to display time remaining in day (with timezone offset)

How to display time remaining until midnight, like this: 22 hours 48 minutes 12 seconds remaining

To make things a bit more difficult, this should show in the UTC+2 timezone. Is this possible in plain javascript or easier with momentjs? Any help appreciated!

Upvotes: 1

Views: 4758

Answers (1)

nikulis
nikulis

Reputation: 134

let now = moment();
let timeDiff = moment(now).utcOffset(120).endOf('day') - now;

let dur = moment.duration(timeDiff);

console.log(`${dur.hours()} hrs ${dur.minutes()} min ${dur.seconds()} sec until midnight.`);
<script src="https://momentjs.com/downloads/moment.min.js"></script>

Upvotes: 4

Related Questions