简体   繁体   English

JS-日期转换为字符串格式

[英]JS - Date to string formatting

I am trying to get the script to display yesterdays date in "m/d/yy" format. 我正在尝试使脚本以“ m / d / yy”格式显示昨天的日期。 When I use this code: 当我使用此代码时:

<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>

this is returned: 这返回:

Here is the comparison report for .

if I put it in without the .format("m/d/yy") , I get this: 如果我不带.format("m/d/yy")放进去,我会得到:

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

What am I doing wrong? 我究竟做错了什么?

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

The date object has many methods for displaying various parts of it. 日期对象具有许多显示其各个部分的方法。 See here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date 参见此处: https : //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Date

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM