jquery datepicker with current time

11,930

Solution 1

From OP's comment,

var d = new Date();
var hours = d.getHours();
var minutes = d.getMinutes();
var seconds = d.getSeconds();
var date = d.getDate();
var month = d.getMonth();
var year = d.getFullYear();
if (month < 10) {
    month = '0' + month;
}
if (date < 10) {
    date = '0' + date;
}
$(".div_date_time").text(year + "-" + month + "-" + date + " " + hours + ":" + minutes + ":" + seconds);

Solution 2

There is a plugin that adds a timepicker to the datepicker: https://github.com/trentrichardson/jQuery-Timepicker-Addon. Also included is a "Now" button that inserts the current date and time.

In your code, just replace datepicker with datetimepicker. You can then modify the date and time formats like so:

$("#datepicker1").datetimepicker({
    dateFormat: "yy-mm-dd",
    timeFormat: "hh:mm:ss"
});
Share:
11,930
Admin
Author by

Admin

Updated on June 11, 2022

Comments

  • Admin
    Admin almost 2 years

    I want to have time along with date in a text box on click of a button (12 hrs format).

    There is the code I have done so far. I am able to get the date but I want to record current time along with the current date.

    $(function() {
        $("#datepicker1").datepicker({
            dateFormat: "yy-mm-dd hh:mm:ss"
        }).datepicker("setDate", "0");
    });
    
    <input id="datepicker1">
    

    hh:mm:ss is what I thought of but its not working. Please guide.

    It can be either via JS or jquery its ok, but on click of button.