0

我想要做的是,例如,如果当地时间是6:00PM我想显示时间提前 10 分钟,而6:10PM其他时间我想从当前时间返回 50 分钟,这样就可以了5:10PM。 . 我到目前为止都没有,因为我只能弄清楚如何显示当前时间

<script>
var currentTime = new Date()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()


var suffix = "AM";
if (hours >= 12) {
suffix = "PM";
hours = hours - 12;
}
if (hours == 0) {
hours = 12;
}

if (minutes < 10)
minutes = "0" + minutes

document.write("<b>" + hours + ":" + minutes + " " + suffix + "</b>")
</script>

如何返回 50 分钟并提前 10 分钟?

4

2 回答 2

1

这应该足够了

<script>
    var futureTime = new Date();
    futureTime.setMinutes(futureTime.getMinutes()+10);

    var pastTime = new Date();
    pastTime.setMinutes(pastTime.getMinutes()-50);
</script>

然后只需将 pastTime 和 futureTime 变量与您现有的显示代码一起使用。

来源:https ://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

于 2013-10-09T09:24:10.747 回答
0
function formatDate(d)
{
    var hours = d.getHours();
    var minutes = d.getMinutes();
    var suffix = "AM";

    if (hours >= 12)
    {
        suffix = "PM";
        hours = hours - 12;
    }
    if (hours == 0)
    {
        hours = 12;
    }

    if (minutes < 10)
    {
        minutes = "0" + minutes;
    }

    return hours + ":" + minutes + " " + suffix;
}

var currentTime = new Date();

var futureTime = new Date(currentTime.getTime());
futureTime.setMinutes(futureTime.getMinutes() + 10);

var pastTime = new Date(currentTime.getTime());
pastTime.setMinutes(pastTime.getMinutes() - 50);

document.write("<b>" + formatDate(currentTime) + "</b>");
document.write("<b>" + formatDate(futureTime) + "</b>");
document.write("<b>" + formatDate(pastTime) + "</b>");
于 2013-10-09T09:46:57.703 回答