Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set values in input type date and time in javascript

Tags:

I have two input fields:

<input type="date" id="currentDate"> <input type="time" id="currentTime"> 

How can I set these fields to the current date/time?

like image 753
DOM manipulator Avatar asked Feb 25 '15 21:02

DOM manipulator


People also ask

How can set current date in input Type date in jquery?

Answers. //Get value of input text var dt = $("input[type=text]"). val(). split('-'); var date = dt[2] +"-"+ dt[1] +"-"+ dt[0];

How do you do 24-hour time input?

input type="time" The time input type is used to create input fields allowing the users to easily enter the time (hours and minutes, sometimes seconds). The value of the time input type is commonly in 24-hour format ("hh:mm").


2 Answers

You can just set the value of the input field with the respective formats:

  • date is yyyy-MM-dd
  • time is HH:mm

Using your example, you can do something simple like:

var date = new Date(); var currentDate = date.toISOString().substring(0,10); var currentTime = date.toISOString().substring(11,16);  document.getElementById('currentDate').value = currentDate; document.getElementById('currentTime').value = currentTime; 
like image 105
jungy Avatar answered Sep 17 '22 07:09

jungy


var date = new Date();  var day = date.getDate(),     month = date.getMonth() + 1,     year = date.getFullYear(),     hour = date.getHours(),     min  = date.getMinutes();  month = (month < 10 ? "0" : "") + month; day = (day < 10 ? "0" : "") + day; hour = (hour < 10 ? "0" : "") + hour; min = (min < 10 ? "0" : "") + min;  var today = year + "-" + month + "-" + day,     displayTime = hour + ":" + min;   document.getElementById('formdate').value = today;       document.getElementById("formtime").value = displayTime; 
like image 21
XepherX Avatar answered Sep 17 '22 07:09

XepherX