Skip to content

Instantly share code, notes, and snippets.

@maptv
Last active July 20, 2023 19:44
Show Gist options
  • Save maptv/aa578dd0fcf562d946b56b185afb40cb to your computer and use it in GitHub Desktop.
Save maptv/aa578dd0fcf562d946b56b185afb40cb to your computer and use it in GitHub Desktop.
Display deciday time with deciday offset and 24-hour time with UTC offset, e.g. 5.50-2 and 13:00-05
// test by running the following in a UNIX shell:
// for tz in 'Pacific/Samoa' 'America/Los_Angeles' 'Etc/UTC' 'Asia/Shanghai' 'Pacific/Kiritimati'; do; echo \\n$tz; env TZ=$tz node deciday.js; done;
const now = new Date(),
year = now.getUTCFullYear(),
month = now.getUTCMonth(),
day = now.getUTCDate(),
hour = now.getUTCHours(),
minute = now.getUTCMinutes(),
second = now.getUTCSeconds(),
millisecond = now.getUTCMilliseconds(),
utc = new Date(year, month, day, hour, minute, second, millisecond),
mid = new Date(year, month, day, 0, 0, 0, 0),
ms_since_midnight = utc - mid,
deciday = ms_since_midnight / 8640000,
minuteOffset = now.getTimezoneOffset(),
sign = minuteOffset > 0 ? "-" : "+",
ddOffset = Math.round(minuteOffset / 144),
hourOffset = Math.abs(minuteOffset / 60).toString().padStart(2, "0"),
paddedHour = now.getHours().toString().padStart(2, "0"),
paddedMinute = now.getMinutes().toString().padStart(2, "0"),
offsetDeciday = deciday - ddOffset,
normalizedDeciday = offsetDeciday - 10 * (offsetDeciday > 10) + 10 * (offsetDeciday < 0),
roundedDeciday = Math.round(normalizedDeciday * 100) / 100,
paddedDeciday = roundedDeciday.toString().padEnd(4, "0");
console.log(`${paddedHour}:${paddedMinute}${sign}${hourOffset}`)
console.log(` ${paddedDeciday}${sign}${Math.abs(ddOffset)}`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment