Last active
April 4, 2023 19:49
-
-
Save nhalase/d85ae2c687dc59f5a4a9f5d10a94980f to your computer and use it in GitHub Desktop.
JS function to generate a Java ZonedDateTime
This file contains 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
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 + "]"; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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]").