Setting current date for a date input type box in a modal

653 0 0 0

Last Updated : 2024-04-18 19:47:26

this snippet will teach you how to set current date and current datetime in input date and input datetime-local specially on fire bootstrap modal

On this snippet I will provide jquery snippet for filling input date or datetime-local with current date or current datetime specially on fire bootstrap modal like this


$(document).on('shown.bs.modal', "#adminModal", function (e)  {
$('#add_item_reading').focus();

// ASSIGN CURRENT DATE TO INPUT TYPE DATE
var now = new Date();
var day = ("0" + now.getDate()).slice(-2);
var month = ("0" + (now.getMonth() + 1)).slice(-2);
var today = (day)+"/"+(month)+"/"+ now.getFullYear();
$('#datePicker').val(today);

// ASSIGN CURRENT DATETIME TO INPUT TYPE DATETIME-LOCAL
let dateControl = document.querySelector('input[type="datetime-local"]');
dateControl.value = new Date().toJSON().slice(0,19);
}) ;

Mahmoud Anwar

Mahmoud Anwar

Back End Developer with a passion for developing innovative web applications that expedite the efficiency and effectiveness of organizational success. Well-versed in technology and writing code to create systems that are reliable and user-friendly. Also has the proven ability to motivate, educate, and collaborate effectively to build web applications and effectively track changes. Confident communicator, strategic thinker, and innovative creator to develop software that is customized to meet a company’s organizational needs, highlight their core competencies, and further their success.