diff --git a/packages/bbui/src/Form/Core/DatePicker.svelte b/packages/bbui/src/Form/Core/DatePicker.svelte
index 8edb68a38e..69bfdda72a 100644
--- a/packages/bbui/src/Form/Core/DatePicker.svelte
+++ b/packages/bbui/src/Form/Core/DatePicker.svelte
@@ -14,16 +14,20 @@
export let value = null
export let placeholder = null
export let appendTo = undefined
+ export let timeOnly = false
const dispatch = createEventDispatcher()
const flatpickrId = `${generateID()}-wrapper`
let open = false
- let flatpickr
+ let flatpickr, flatpickrOptions, isTimeOnly
+
+ $: isTimeOnly = !timeOnly && value ? !isNaN(new Date(`0-${value}`)) : timeOnly
$: flatpickrOptions = {
element: `#${flatpickrId}`,
- enableTime: enableTime || false,
+ enableTime: isTimeOnly || enableTime || false,
+ noCalendar: isTimeOnly || false,
altInput: true,
- altFormat: enableTime ? "F j Y, H:i" : "F j, Y",
+ altFormat: isTimeOnly ? "H:i" : enableTime ? "F j Y, H:i" : "F j, Y",
wrap: true,
appendTo,
disableMobile: "true",
@@ -35,6 +39,11 @@
if (newValue) {
newValue = newValue.toISOString()
}
+ // if time only set date component to today
+ if (timeOnly) {
+ const todayDate = new Date().toISOString().split("T")[0]
+ newValue = `${todayDate}T${newValue.split("T")[1]}`
+ }
dispatch("change", newValue)
}
@@ -67,7 +76,11 @@
return null
}
let date
- if (val instanceof Date) {
+ let time = new Date(`0-${val}`)
+ // it is a string like 00:00:00, just time
+ if (timeOnly || (typeof val === "string" && !isNaN(time))) {
+ date = time
+ } else if (val instanceof Date) {
// Use real date obj if already parsed
date = val
} else if (isNaN(val)) {
@@ -77,7 +90,7 @@
// Treat as numerical timestamp
date = new Date(parseInt(val))
}
- const time = date.getTime()
+ time = date.getTime()
if (isNaN(time)) {
return null
}
@@ -88,69 +101,71 @@
}
-