0

i have a field where you have to enter the date but i also have a script on that field which automatically inserts the current date. The problem i have is that the date is formatted like M/DD/YYYY 00:00:00 AM/PM but i just wanted it to be like DD/MM/YY.

Here is my current code by the way :

<b>Date of Registration:</b> <input type="text" id="txtregodate" name="regodate">

<script type="text/javascript">
var now = new Date ();
document.getElementById('txtregodate').value = now.toLocaleString();
</script>

Is there a way to format that function i'm using in that way?

bigsenator
  • 19
  • 1
  • 7

3 Answers3

1

There is a great library for this stuff: http://momentjs.com/

Bas Slagter
  • 9,831
  • 7
  • 47
  • 78
1

Try this... this will give DD/MM/YY format date

var now = new Date ();
document.getElementById('txtregodate').value = now.getDate() + '/' + (now.getMonth() + 1)+ '/' + (now.getYear()%100);
999k
  • 6,257
  • 2
  • 29
  • 32
0

(It's all about utilising the Date methods (getDate, getMonth, getYear, getFullYear) and concatenating them as strings. That's basically it.

var now = new Date();
document.getElementById('txtregodate').value = now.getDate()+'/'+(now.getMonth()+1)+'/'+(now.getFullYear()%100);

You can get this info from here

marksyzm
  • 5,281
  • 2
  • 29
  • 27