Open1
JavaScriptの標準APIでシンプルな日時をパースする
const parseDatetime = (input) => {
// 2020-01-01T00:00Z, 2020-01-01T00:00+09:00, 2020-01-01T00:00
const datetimeRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(Z|[+-]\d{2}:\d{2})?$/
const result = input.match(datetimeRegex)
if (result !== null) {
const datetime = input + (result[1] ? '' : '+09:00')
const date = new Date(datetime)
if (!Number.isNaN(date)) {
return date
}
}
throw new Error('Invalid datetime')
}