在JavaScript中格式化getDate()

时间:2012-11-09 22:59:35

标签: javascript getdate

我一直在玩这个JavaScript,以便在我的日期输出中获得正确的格式,但没有运气。我需要将此代码显示为Month Day,Year。截至目前,它显示dd / mm / yy。

以下是我正在使用的脚本。

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

2 个答案:

答案 0 :(得分:1)

以下是another post here on Stack Overflow的修改。我想这会做你想要的。

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+"'>");

答案 1 :(得分:0)

我真的不懂。这是你正在寻找的吗?

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