Last active
February 8, 2024 09:30
-
-
Save heiwa4126/4c3baf77b84d67237ced73d5d9878e81 to your computer and use it in GitHub Desktop.
JavaScriptでDateオブジェクトからISO 8601形式の時刻文字列(YYYY-MM-DDTHH:mm:ss±HHmm)を得る関数。PythonのDatetie.strftime()みたいのが無いので面倒。
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 getLocalISOWithTZ(date) { | |
const tzOffset = date.getTimezoneOffset(); | |
const absOffset = Math.abs(tzOffset); | |
const tzSign = tzOffset >= 0 ? "-" : "+"; | |
const tzHours = String(Math.floor(absOffset / 60)).padStart(2, "0"); | |
const tzMinutes = String(absOffset % 60).padStart(2, "0"); | |
const adjustedDate = new Date(date.getTime() - tzOffset * 60 * 1000); | |
const isoString = adjustedDate.toISOString(); | |
const dateString = isoString.slice(0, 10); | |
const timeString = isoString.slice(11, 19); | |
return `${dateString}T${timeString}${tzSign}${tzHours}${tzMinutes}`; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
こんなのメンテしきれないのでdate-fnsで
のほうがいいです。date-fns - npm