0

I want to get current date in text field of jsp, once open. Please someone help me

document.getElementById("date").value = new Date().toJSON().slice(0,10)
<body>
<form>
<header><h1>Testing </h1></header>
Receipt number: <input type="text" id="grn" class="tb1" onkeypress="return isNumber(event)" />
Irep No <input type="text" name="irepno" id="irepno" class="tb1" maxlength="8"/>
Irep date <input type="hidden" id="date"/>

<br></br>
</form>
</body>
gyre
  • 16,369
  • 3
  • 37
  • 47
Jsel
  • 199
  • 3
  • 11
  • Hi, your code seems to be working fine. `` text box will be filled with current date. You can view it by changing the type from **hidden** to **text**. `` – Jijo Cleetus Mar 01 '17 at 04:54
  • i didn't get any date value after change as text also! – Jsel Mar 01 '17 at 05:21

1 Answers1

0

Your problem in here is date format with Javascript. As I see the input have id="date" is a hidden input.

To set the value for this input by format dd/mm/yyyy use need change Js code like:

var now = new Date();
document.getElementById("date").value = (now.getDate()>9?'':'0')+now.getDate()+'/'+ (now.getMonth()>8?'':'0')+(now.getMonth() + 1)+'/'+now.getFullYear();
dphung duy
  • 19
  • 5
  • The value for an input type date must be a [*validate date*](https://html.spec.whatwg.org/multipage/infrastructure.html#concept-date) in the format YYYY-MM-DD. – RobG Mar 01 '17 at 06:59