JS - 日期到字符串格式

时间:2014-03-28 22:46:57

标签: javascript datetime formatting

我试图让脚本以“m / d / yy”格式显示昨天的日期。当我使用这段代码时:

<p>
    Here is the report for 
    <script>
        var yesterday = new Date();
        yesterday.setDate(yesterday.getDate() - 1);
        document.write(yesterday.format("m/d/yy"));
    </script>
    .
</p>

返回:

Here is the comparison report for .

如果我在没有.format("m/d/yy")的情况下把它放进去,我就明白了:

Here is the comparison report for Thu Mar 27 2014 15:42:31 GMT-0700 (Pacific Daylight Time) .

我做错了什么?

1 个答案:

答案 0 :(得分:0)

document.write(yesterday.getMonth() + "/" + yesterday.getDay() + "/" +  yesterday.getFullYear())

日期对象有很多方法可以显示它的各个部分。看这里: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date