Skip to content

Instantly share code, notes, and snippets.

@HerbertLim
Last active February 11, 2019 06:14
Show Gist options
  • Save HerbertLim/a28c490025a45e476862ef6a49f49331 to your computer and use it in GitHub Desktop.
Save HerbertLim/a28c490025a45e476862ef6a49f49331 to your computer and use it in GitHub Desktop.
Get UTC hours and minutes from local time format, "08:15"
// parameter hmTime is a string containing 5 characters,
// first two is hours, last to is minutes, which are divied with ':',
// e.g., "08:15", "17:43"
const _ = require('lodash');
function localHhMmToUtc(hmTime) {
const splitted = _.split(hmTime, ':')
const localHours = splitted[0];
const localMinutes = splitted[1];
const date = new Date()
const now = new Date(date.getFullYear(), date.getMonth(), date.getDate(), localHours, localMinutes) // UTC
const utcHours = now.getUTCHours()
const utcMinutes = now.getUTCMinutes()
return {utcHours, utcMinutes} // returns an object containing UTC hours and minutes, respectively.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment