How to show current date in Html date field

 Date field default date in HTML

  


codingzon-defaultdate-in-html-date-thumb


 

In this tutorial, we learn How to set up current date as a default date value in HTML date field using JavaScript. It is visible when a page is loaded.

Here is an example:


In this code, we used the JavaScript date to generate a date and set that date to the HTML date field.

MOVETOP↑

 

index.php

 

<html>
<head>
<title>Codingzon Tutorials</title>
 

<body bgcolor="">
<center>

<p id="datetime"> </p>

<form>
Current Date: <input type="date" name="date1" id="date1" plaseholder="Date1" ><br/>
 
</form>

<script>
 
// ***************** Get current date and time in JavaScript ***************** //

var now = new Date();

// reading each part of datetime 

var day = now.getDate();
var month = now.getMonth() + 1;
var year = now.getFullYear();

if (month < 10) month = "0" + month;
if (day < 10) day = "0" + day;

//making a dateFormat string(YYYY-MM-DD)

var today = year + "-" + month + "-" + day;       
document.getElementById("date1").value = today;

 
</script>

</body>
</html>
 
Result:
 
 
codingzon-defaultdate-in-html-date


 


Comments

Popular posts from this blog

Using javascript pass form variables to iframe src

Creating a new PDF by Merging PDF documents using TCPDF

Import excel file into mysql in PHP