Question

I have been playing around with this JavaScript to get the right format on my date output, but with no luck. I need this code to show as Month Day, Year. As of now it's showing dd/mm/yy.

Below is the script I'm using.

<script>
var tD = new Date();
var datestr = tD.getDate() + (tD.getMonth()+ 1)  + ", " + tD.getFullYear();
document.write("<input type='hidden' name='date' value='"+datestr+"'>");
</script>
Was it helpful?

Solution

Here is a modification of another post here on Stack Overflow. I think this will do what you want.

var tD = new Date();
var monthNames = [ "January", "February", "March", "April", "May", "June",
   "July", "August", "September", "October", "November", "December" ];
var datestr = monthNames[tD.getMonth()] + " " + tD.getDate()  + ", " + tD.getFullYear();
document.write("<input type='hidden' name='date' value='"+datestr+"'>");

OTHER TIPS

I don't really get it. Is this, what you're looking for?

<script>
var tD = new Date();
var datestr = (tD.getMonth() + 1) + ' ' + tD.getDay() + ", " + tD.getFullYear();
document.write("<input type='hidden' name='date' value='"+datestr+"'>");
</script>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top