Reputation: 385
I have an input of type date
, where I use materialize to pick a date. I want to have the current date as default on init.
HTML
<input formControlName="invoice_date" id="invoice_date" type="date" class="datepicker" materialize="pickadate" [materializeParams]="[{ format: 'yyyy-mm-dd', formatSubmit: 'yyyy-mm-dd',
closeOnSelect: true, selectMonths: true, selectYears: true, today: '',
max: true, onSet: onSetDatepicker }]">
JavaScript
onSetDatepicker(date) {
if (date.select) {
$('#invoice_date').pickadate().pickadate('picker').close();
}
}
Upvotes: 1
Views: 14077
Reputation: 273
var date = new Date();
var day = ("0" + date.getDate()).slice(-2);
var month = ("0" + (date.getMonth() + 1)).slice(-2);
var today = date.getFullYear() + "-" + (month) + "-" + (day);
$('#dateid').val(today);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="date" id="dateid">
Upvotes: 2
Reputation: 3759
Maybe this can help your problem in setting the default value of date as current date.
<input type="text" class="datepicker">
<script>
document.addEventListener('DOMContentLoaded', function () {
let today = new Date().toLocaleDateString();
var options = {
defaultDate: new Date(today),
setDefaultDate: true
};
var elems = document.querySelector('.datepicker');
var instance = M.Datepicker.init(elems, options);
instance.setDate(new Date(today));
});
</script>
How do I get the current date in JavaScript?
Upvotes: 0
Reputation: 2414
The datepicker libray usually have the current day as its default starting point, so I'm not sure why you have it differently. However, what you could do is set the value
of the input field to contain the date of today.
Example:
<input formControlName="invoice_date" id="invoice_date" type="date" class="datepicker"
materialize="pickadate" [materializeParams]="[{
format: 'yyyy-mm-dd', formatSubmit: 'yyyy-mm-dd',
closeOnSelect: true, selectMonths: true, selectYears: true, today: '',
max: true, onSet: onSetDatepicker }]"
value="<?php echo date('y-m-d'); ?>">
Upvotes: 0