Created
August 7, 2021 14:28
-
-
Save mattcarlotta/ebfb42a71a545e0ff942bf38d7f15751 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function timeZone(date = new Date()) { | |
const offset = date.getTimezoneOffset(); | |
const absOffset = Math.abs(offset); | |
const hours = Math.floor(absOffset / 60); | |
const minutes = absOffset % 60; | |
const minutesOut = minutes > 0 ? ':' + ('0' + minutes).slice(-2) : ''; | |
return (offset < 0 ? '+' : '-') + hours + minutesOut; | |
} | |
export default function dateTime(options = {}) { | |
let { | |
date = new Date(), | |
local = true, | |
showTimeZone = false, | |
showMilliseconds = false | |
} = options; | |
if (local) { | |
// Offset the date so it will return the correct value when getting the ISO string. | |
date = new Date(date.getTime() - (date.getTimezoneOffset() * 60000)); | |
} | |
let end = ''; | |
if (showTimeZone) { | |
end = ' UTC' + (local ? timeZone(date) : ''); | |
} | |
if (showMilliseconds && date.getUTCMilliseconds() > 0) { | |
end = ` ${date.getUTCMilliseconds()}ms${end}`; | |
} | |
return date | |
.toISOString() | |
.replace(/T/, ' ') | |
.replace(/\..+/, end); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment