Skip to content

Instantly share code, notes, and snippets.

@masahirompp
Last active September 24, 2021 07:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masahirompp/4dc521f60d75d1020288093e1fc6f76a to your computer and use it in GitHub Desktop.
Save masahirompp/4dc521f60d75d1020288093e1fc6f76a to your computer and use it in GitHub Desktop.
Pure JavaScript JST Time
// どの環境で実行しても、JST時刻を取得する
const timeZoneOffset = 9 // JST
const getJstDate = () => {
const time = Date.now() // UTC millisecond
const date = new Date(time) // local date
const utcHours = date.getUTCHours() // UTC Hour
date.setHours(utcHours + timeZoneOffset) // UTC Hour + TimeZone Offset
return {
time,
tzYear: date.getFullYear(),
tzMonth: date.getMonth() + 1,
tzDate: date.getDate(),
tzDay: date.getDay()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment