Skip to content

Instantly share code, notes, and snippets.

@nhalase
Last active April 4, 2023 19:49
Show Gist options
  • Save nhalase/d85ae2c687dc59f5a4a9f5d10a94980f to your computer and use it in GitHub Desktop.
Save nhalase/d85ae2c687dc59f5a4a9f5d10a94980f to your computer and use it in GitHub Desktop.
JS function to generate a Java ZonedDateTime
const pad = (value) => {
return value < 10 ? '0' + value : value;
}
const createOffset = (date) => {
const sign = (date.getTimezoneOffset() > 0) ? "-" : "+";
const offset = Math.abs(date.getTimezoneOffset());
const hours = pad(Math.floor(offset / 60));
const minutes = pad(offset % 60);
return sign + hours + ":" + minutes;
}
const createZonedDateTime = (date = new Date()) => {
return date.getFullYear() + "-" + pad(parseInt(date.getMonth(), 10) + 1) + "-" + pad(date.getDate()) + "T" + pad(date.getHours()) + ":" + pad(date.getMinutes()) + ":" + pad(date.getSeconds()) + createOffset(date) + "[" + Intl.DateTimeFormat().resolvedOptions().timeZone + "]";
};
@nhalase
Copy link
Author

nhalase commented Apr 4, 2023

Should return a JVM-formatted ZonedDateTime string using the brower's timezone via Intl (e.g., "2023-04-04T14:48:26-05:00[America/Chicago]").

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment