user2541120
user2541120

Reputation:

jquery datepicker with current time

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.

Upvotes: 6

Views: 14137

Answers (2)

J.C. Yamokoski
J.C. Yamokoski

Reputation: 1014

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"
});

Upvotes: 1

Praveen
Praveen

Reputation: 56509

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);

Upvotes: 1

Related Questions