paul724
paul724

Reputation: 144

flatpickr configuration - how to set a default date of today's date for a new record but retain stored value when updating an existing record

I am successfully inserting or updating a date record using flatpickr. With a new record the default is 1/1/1970 but I want to make the default date for a new record today's date. See code below. Initially I only used the code that is now in the "else" part of the statement but this reset the date on any record being edited to today's date. So second idea was to test if this is an existing record in which case the first two lines of the "if" statement work or if it is a new record then the "else" set's defaultDate to today. Current behaviour with this code is that adding and editing is working fine but on a new record the default date is 1/1/1970 not today's date. Thank you

if ($("#date_record_created").length > 0) 
{
    $("#date_record_created").flatpickr({
    enableTime: false,
    dateFormat: "d-M-Y"//"d-m-Y"//"F, d Y" 
    //minDate: "01-Jan-2012"
    });
}  
else 
{
    $("#date_record_created").flatpickr({
    enableTime: false,
    dateFormat: "d-M-Y",//"d-m-Y"//"F, d Y"
    defaultDate: "today",
    dateFormat: "d-M-Y"    
    });
};

Upvotes: 2

Views: 7359

Answers (1)

joao-m-santos
joao-m-santos

Reputation: 187

defaultDate: new Date()

should do the trick. It points the defaultDate value to a new JavaScript Date object that, by default, holds the current date. (See below)

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

If no arguments are provided, the constructor creates a JavaScript Date object for the current date and time according to system settings for timezone offset.

Upvotes: 2

Related Questions