Skip to content

Instantly share code, notes, and snippets.

@mattcarlotta
Created August 7, 2021 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattcarlotta/ebfb42a71a545e0ff942bf38d7f15751 to your computer and use it in GitHub Desktop.
Save mattcarlotta/ebfb42a71a545e0ff942bf38d7f15751 to your computer and use it in GitHub Desktop.
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