Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Web Technology by (20.3k points)

I would like to add the current date to a hidden HTML tag so that it can be sent to the server:

<input type="hidden" id="DATE" name="DATE" value="WOULD_LIKE_TO_ADD_DATE_HERE">

How can I add a formatted date to the VALUE attribute?

1 Answer

0 votes
by (40.7k points)

You can try using the code given below:

var today = new Date();

var dd = today.getDate();

var mm = today.getMonth() + 1; //January is 0!

var yyyy = today.getFullYear();

if (dd < 10) {

  dd = '0' + dd;

if (mm < 10) {

  mm = '0' + mm;

var today = dd + '/' + mm + '/' + yyyy;

document.getElementById('DATE').value = today;

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Jan 28, 2021 in Java by dante07 (13.1k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...