Advance usage of Month range picker
Change date format
change the date format as you like
You can check supported date format characters here
<script>
import svlatepickr from 'svelte-flatpickr-plus';
const options = {
isMonthPicker: true,
mode: 'range',
dateFormat: 'Z' /*set as ISO Date format**/
};
</script>
<input name="dateFormat" use:svlatepickr={options} />
Result:
Value:
Please select date from above input.
Alt input
change the date format as you like , unaffected a real value
You can check supported date format characters here
<script>
import svlatepickr from 'svelte-flatpickr-plus';
const options = {
isMonthPicker: true,
mode: 'range',
dateFormat: 'Z' /*set as ISO Date format**/,
altInput: true,
altFormat: 'F Y'
};
</script>
<input name="altInput" use:svlatepickr={options} />
Result:
Value:
Please select date from above input.
Set default date
Set a default date to input after component is mounted
If select only one date and click out side of calendar with defaultDate
options provided, this will reset a select date range to a default date that provided, if not want it reset into a default date just passing additional options resetToDefault
as false
.
If sending more than 2 date data in defaultDate
options flatpickr will using only the first two date data in array as a default date
<script>
import svlatepickr from 'svelte-flatpickr-plus';
const todayDate = new Date();
const options = {
isMonthPicker: true,
mode: 'range',
dateFormat: 'Z' /*set as ISO Date format**/,
altInput: true,
altFormat: 'F Y',
defaultDate: [
/**Set default date as today month to next 2 month*/
getFirstDaysOfMonth(0),
getFirstDaysOfMonth(2)
]
};
function getFirstDaysOfMonth(offset) {
const date = new Date();
date.setDate(1);
date.setMonth(date.getMonth() + offset);
date.setHours(0, 0, 0, 0, 0);
return date.toISOString();
}
</script>
<input name="altInput" use:svlatepickr={options} />
Result:
Value:
Please select date from above input.
Set min/max date
Set a min/max date to a date picker
Be careful while setting a minDate
please rechecking that minDate is starting from a first date of the month
if it not it will cause a problem that you can not select a month even it available to selected.
<script>
import svlatepickr from 'svelte-flatpickr-plus';
const options = {
isMonthPicker: true,
mode: 'range',
dateFormat: 'Z' /*set as ISO Date format**/,
altInput: true,
altFormat: 'F Y',
minDate: getFirstDaysOfMonth(-2) /*set min month to 2 month ago**/,
maxDate: getFirstDaysOfMonth(2) /*set max month to 2 month later**/
};
function getFirstDaysOfMonth(offset) {
const date = new Date();
date.setDate(1);
date.setMonth(date.getMonth() + offset);
date.setHours(0, 0, 0, 0, 0);
return date.toISOString();
}
</script>
<input name="minMaxDate" use:svlatepickr={options} />
Result:
Value:
Please select date from above input.
Set disable date
Disable any date in calendar
<script>
import svlatepickr from 'svelte-flatpickr-plus';
const options = {
isMonthPicker: true,
mode: 'range',
dateFormat: 'Z' /*set as ISO Date format**/,
altInput: true,
altFormat: 'F Y',
disable: [
getFirstDaysOfMonth(-2),/*disable a month that 2 days ago**/
getFirstDaysOfMonth(-2)/*disable a month that 2 days later**/
]
};
function getFirstDaysOfMonth(offset) {
const date = new Date();
date.setDate(1);
date.setMonth(date.getMonth() + offset);
date.setHours(0, 0, 0, 0, 0);
return date.toISOString();
}
</script>
<input name="disableDate" use:svlatepickr={options} />
Result:
Value:
Please select date from above input.
Hooks
Build a date picker with hooks options
You can check all supported hooks name here
<script>
import svlatepickr from 'svelte-flatpickr-plus';
const options = {
isMonthPicker: true,
mode: 'range',
dateFormat: 'Z' /*set as ISO Date format**/,
altInput: true,
altFormat: 'F Y',
onChange: (selectedDates, dateStr) => {
console.log('on change hook!');
console.log(selectedDates[0]);
console.log(dateStr);
},
onOpen: (selectedDates, dateStr) => {
console.log('on open hook!');
},
onClose: (selectedDates, dateStr) => {
console.log('on close hook!');
}
};
</script>
<input name="hooks" use:svlatepickr={options} />
Result: open developer tools and checking console for hooks results
Value:
Please select date from above input.
Listen to events
Listen to event of the date picker from input and with that you can get
selectedDates
, dateStr
or instance
data with detail
object from callback Event data
All supported events name is just like as hooks name but in all lower case.
you can check all supported hooks name here
With onchange
event you can not access to detail
object because
onchange
event itself is a DOM Native Event that cannot modify and adding detail
object.
By the way you can using onvalueupdate
event instead to access to detail
object from callback Event data
or you can using hooks approach instead
<script>
import svlatepickr from 'svelte-flatpickr-plus';
const options = {
isMonthPicker: true,
mode: 'range',
dateFormat: 'Z' /*set as ISO Date format**/,
altInput: true,
altFormat: 'F Y'
};
const changeHandler =(event)=>{
console.log('event change!')
}
const valueUpdateHandler = (event) => {
if (event.detail) {
const { selectedDates, dateStr, instance } = event.detail;
console.log('event value update!', { selectedDates, dateStr, instance });
}
};
const openHandler = (event) => {
console.log('event open!');
};
const closeHandler = (event) => {
console.log('event close!');
};
</script>
<input
name="eventDate"
onchange={changeHandler}
onvalueupdate={valueUpdateHandler}
onopen={openHandler}
onclose={closeHandler}
use:svlatepickr={options}
/>
Result: open developer tools and checking console for events results
Value:
Please select date from above input.
Reference to a DOM node
You can binding the DOM node of date picker with bind:this
directive and you can access a flatpickr instance with ._flatpickr
.
With access to instance of flatpickr you can using any properties and method that flatpickr provide here
<script>
import svlatepickr from 'svelte-flatpickr-plus';
let datePickerElement;
const options = {
isMonthPicker: true,
mode: 'range',
dateFormat: 'Z' /*set as ISO Date format**/,
altInput: true,
altFormat: 'F Y'
};
function clearDatePicker(){
datePickerElement._flatpickr.clear();
}
</script>
<input name="domDate"
bind:this={datePickerElement}
use:svlatepickr={options} />
<button onclick={clearDatePicker}> Clear </button>
Result:
Value:
Please select date from above input.
Control clear behavior
Control on .clear()
behavior with resetMoveDefault
and resetToDefault
in options
with resetMoveDefault
as true
(by default) while instance of date picker got reset with .clear()
it will move calendar to a default (or today date if defaultDate
not set in options), if it set to false
this the calendar will stay on a latest position of a date before.
with resetToDefault
as true
(by default) while instance of date picker got reset with .clear()
a value in input DOM will be reset back to a default date (or empty string if it set to be false
or defaultDate
not set in options)
<script>
import svlatepickr from 'svelte-flatpickr-plus';
const todayDate = new Date();
const options = {
isMonthPicker: true,
mode: 'range',
dateFormat: 'Z' /*set as ISO Date format**/,
altInput: true,
altFormat: 'F Y',
defaultDate: new Date(),
resetMoveDefault: false,
resetToDefault: false
};
</script>
<input name="clearBehaviorDate" use:svlatepickr={options} />
Result:
Value:
Please select date from above input.
External element
Can using a datepicker action on external element (aka. wrapper element) such as
div
by passing wrap: true
as a additional option and adding data-input
as a attribute to input element.
The selector for flatpickr should be the wrapping div with class
flatpickr
not the input
tag.
<script>
import svlatepickr from 'svelte-flatpickr-plus';
const options = {
isMonthPicker: true,
mode: 'range',
dateFormat: 'Z' /*set as ISO Date format**/,
altInput: true,
altFormat: 'F Y',
wrap: true
};
</script>
<div use:svlatepickr={options}>
<input name="wrapDate" data-input />
<button type="button" title="toggle" data-toggle> toggle </button>
<button type="button" title="clear" data-clear> clear </button>
</div>
Result:
Value:
Please select date from above input.
Localize date picker
In this example will using Thai language by adding locale: Thai
in options
while Thai
is imported from l10n
and using Buddhist era by adding useLocaleYear: true
in options
You can check supported locales here
If your localize file giving wrong locale year please let me know by open a issue here with tag localize
<script>
import svlatepickr from 'svelte-flatpickr-plus';
import { l10n } from '$lib';
const thaiLocale = l10n.th;
const options = {
isMonthPicker: true,
mode: 'range',
dateFormat: 'Z' /*set as ISO Date format**/,
altInput: true,
altFormat: 'F Y',
locale: thaiLocale,
useLocaleYear: true
};
</script>
<input name="localizeDate" use:svlatepickr={options} />
Result:
Value:
Please select date from above input.
Using in form action
<script>
import svlatepickr from 'svelte-flatpickr-plus';
import {formDataToJson} from '$lib/utils.js'
let informDatePickerElement;
const altInputOptions = {
isMonthPicker: true,
mode: 'range',
dateFormat: 'Z' /*set as ISO Date format**/,
altInput: true,
altFormat: 'F Y',
};
const defaultDateOptions ={
...altInputOptions,
defaultDate: new Date()
}
const wrapOptions ={
...altInputOptions,
wrap: true
}
const beforeSubmitHandler = ({ formElement, formData, cancel }) => {
formDataJSON = formDataToJson(formData)
cancel();
formElement.reset();
}
</script>
<form name="datePickerForm" method="POST" use:enhance={beforeSubmitHandler} >
<section>
<label for="datePicker_1">Month range picker:
<br />
<input name="datePicker_1" use:svlatepickr={altInputOptions}>
</section>
<br />
<section>
<label for="datePicker_2">Month range picker with initial date: </label>
<br />
<input id="datePicker_2" name="datePicker_2" use:svlatepickr={defaultDateOptions} />
</section>
<br />
<section>
<label for="datePicker_3">Month range picker with flatpickr element binding: </label>
<br />
<input
id="datePicker_3"
name="datePicker_3"
bind:this={informDatePickerElement}
use:svlatepickr={defaultDateOptions}
/>
<button type="button" onclick={()=>{informDatePickerElement._flatpickr.clear()}}>
Clear
</button>
</section>
<br />
<section>
<label for="datePicker_4">Month range picker with wrapper</label>
<br />
<div use:svlatepickr={wrapOptions}>
<input id="datePicker_4" name="datePicker_4" data-input />
<button type="button" title="toggleInFrom" data-toggle> toggle </button>
<button type="button" title="clearInFrom" data-clear> clear </button>
</div>
</section>
<br />
<div>
<div>
<button type="submit">Submit</button>
<button type="reset">Reset</button>
</div>
</div>
</form>
Result :
Form data JSON:
Please submit form above