1

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>
halfer
  • 19,824
  • 17
  • 99
  • 186
  • See [MDN](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date#Methods) for reference on all `Date` object methods. – Marat Tanalin Nov 09 '12 at 23:04

2 Answers2

1

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+"'>");
Community
  • 1
  • 1
Joshua Dwire
  • 5,415
  • 5
  • 29
  • 50
  • Why not just link to the post? :) – Robin Drexler Nov 09 '12 at 23:05
  • 1
    @Robin Because that post doesn't do exactly what the OP wanted. I wanted to be more helpful by giving him exactly the code he should use, and because link-only posts are discouraged even if there just links to other posts. See http://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers. – Joshua Dwire Nov 09 '12 at 23:08
  • freaking awesome this worked perfectly. thats why you guys are the experts. – Erick Hernandez Nov 09 '12 at 23:10
0

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>
Robin Drexler
  • 4,307
  • 3
  • 25
  • 28