|
|
|
@ -15,6 +15,7 @@ |
|
|
|
export let placeholder = null |
|
|
|
export let appendTo = undefined |
|
|
|
export let timeOnly = false |
|
|
|
export let ignoreTimezones = false |
|
|
|
|
|
|
|
const dispatch = createEventDispatcher() |
|
|
|
const flatpickrId = `${uuid()}-wrapper` |
|
|
|
@ -50,19 +51,35 @@ |
|
|
|
|
|
|
|
const handleChange = event => { |
|
|
|
const [dates] = event.detail |
|
|
|
const noTimezone = enableTime && !timeOnly && ignoreTimezones |
|
|
|
let newValue = dates[0] |
|
|
|
if (newValue) { |
|
|
|
newValue = newValue.toISOString() |
|
|
|
} |
|
|
|
// if time only set date component to 2000-01-01 |
|
|
|
|
|
|
|
// If time only set date component to 2000-01-01 |
|
|
|
if (timeOnly) { |
|
|
|
newValue = `2000-01-01T${newValue.split("T")[1]}` |
|
|
|
} |
|
|
|
// date only, offset for timezone so always right date |
|
|
|
|
|
|
|
// For date-only fields, construct a manual timestamp string without a time |
|
|
|
// or time zone |
|
|
|
else if (!enableTime) { |
|
|
|
const year = dates[0].getFullYear() |
|
|
|
const month = `${dates[0].getMonth() + 1}`.padStart(2, "0") |
|
|
|
const day = `${dates[0].getDate()}`.padStart(2, "0") |
|
|
|
newValue = `${year}-${month}-${day}T00:00:00.000` |
|
|
|
} |
|
|
|
|
|
|
|
// For non-timezone-aware fields, create an ISO 8601 timestamp of the exact |
|
|
|
// time picked, without timezone |
|
|
|
else if (noTimezone) { |
|
|
|
const offset = dates[0].getTimezoneOffset() * 60000 |
|
|
|
newValue = new Date(dates[0].getTime() - offset).toISOString() |
|
|
|
newValue = new Date(dates[0].getTime() - offset) |
|
|
|
.toISOString() |
|
|
|
.slice(0, -1) |
|
|
|
} |
|
|
|
|
|
|
|
dispatch("change", newValue) |
|
|
|
} |
|
|
|
|
|
|
|
@ -112,10 +129,12 @@ |
|
|
|
// Treat as numerical timestamp |
|
|
|
date = new Date(parseInt(val)) |
|
|
|
} |
|
|
|
|
|
|
|
time = date.getTime() |
|
|
|
if (isNaN(time)) { |
|
|
|
return null |
|
|
|
} |
|
|
|
|
|
|
|
// By rounding to the nearest second we avoid locking up in an endless |
|
|
|
// loop in the builder, caused by potentially enriching {{ now }} to every |
|
|
|
// millisecond. |
|
|
|
|