Search code examples
javascriptjqueryhtmlflatpickr

Set current date as "today" in the result using Flatpickr


I am using Flatpickr for my date picker. however, when i select today's date, i want the result to be displayed as "Today".

jQuery(".datepicker").flatpickr({
    wrap: true,
    altInput: true,
    altFormat: "F j, Y",
    dateFormat: "Y-m-d",
    defaultDate: "today",
    instance.set('defaultDate', 'today');
});

this is what i have tried so far. the part instance.set isn't working for me. maybe i did something wrong.

Below is the snippet of the current working code. all i need is the current displayed date to be "Today" not the date itself.

jQuery(function() {
	initDatePicker();
});


function initDatePicker() {
	jQuery(".datepicker").flatpickr({
		wrap: true,
		altInput: true,
		altFormat: "F j, Y",
		dateFormat: "Y-m-d",
		defaultDate: "today"
	});
}
<link href="https://cdn.jsdelivr.net/npm/flatpickr/dist/flatpickr.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/flatpickr"></script>
<script>
  
</script>

<div class="input-group datepicker">
  <input type="text" class="form-control" data-input aria-describedby="date1">
  <div class="input-group-append">
    <button class="btn btn-secondary" type="button" id="date1" title="toggle" data-toggle><i class="icon-angle-down-4 mr-0"></i></button>
  </div>
</div>


Solution

  • @Lucian, i've found solution in handling onValueUpdate and onReady (for initial check, because defaultDate can be today, as in your case) events and updating value of private field _input.value.

    Although, i didn't find any way to set "Today" word in the field without breaking incapsulation - because this word cannot be parsed to Date object and throws error.

    My code for you is here. Hope this helps!