为何不显示时间?

时间:2013-10-09 16:48:22

标签: javascript

<script>
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);
</script>

我想要做的就是当前时间为6:00PM,在此之前50分钟显示5:10PM并且我还试图在当前时间后10分钟显示另一个时间时间是6:10PM ..我该怎么做才能将其格式化为X:XXPM

我把上面的代码放到我的页面中,我什么都没看到。

3 个答案:

答案 0 :(得分:1)

document.write(currentTime + '<br>' + futureTime + '<br>' + pastTime);

var str = currentTime + '<br>' + futureTime + '<br>' + pastTime;
document.getElementById('myDiv').innerHTML = str;

var str = currentTime + '<br>' + futureTime + '<br>' + pastTime;
document.getElementById('myDiv').appendChild(str);

document.getElementById('myDiv1').appendChild(pastTime);
document.getElementById('myDiv2').appendChild(currentTime);
document.getElementById('myDiv3').appendChild(futureTime);

你的html看起来像

<div id='myDiv1'></div>
<div id='myDiv2'></div>
<div id='myDiv3'></div>

更新

格式化日期 - 这是一个不同的问题 - 已经被无数帖子回答了。

Here's a great one.

10 ways to format time and date using JavaScript.

注意

Jason Sperske发表评论我不能忽视 - moment.js太棒了。

答案 1 :(得分:0)

您需要将值发送到某些HTML元素:

HTML:

<p id="future"></p>
<p id="past"></p>

使用Javascript:

function formatTime(dateObj){

    var dateString = "";
    var hour = dateObj.getHours();  
    var minutes = dateObj.getMinutes();
    var pm = false;

    if (hour > 12)
        pm = true;

    if (pm === true){
        var pmHour = (hour>=13) ? hour-12:hour;
        dateString += pmHour.toString();
        dateString += ":";
        dateString += minutes.toString();
        dateString += " pm";
    }

    else{
        dateString += hour.toString();
        dateString += ":";
        dateString += minutes.toString();
        dateString += " am";
    }

    return dateString;
}

var currentTime = new Date();

var futureTime = new Date(currentTime.getTime());
futureTime.setMinutes(futureTime.getMinutes() + 10);
var f = document.getElementById("future");
f.innerHTML = formatTime(futureTime);

var pastTime = new Date(currentTime.getTime());
pastTime.setMinutes(pastTime.getMinutes() - 50);
var f = document.getElementById("past");
f.innerHTML = formatTime(pastTime);

<强> JSFiddle Example

答案 2 :(得分:0)

以下是解决方案:

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

function formatAMPM(date) {
  var hours = date.getHours();
  var minutes = date.getMinutes();
  var ampm = hours >= 12 ? 'PM' : 'AM';
  hours = hours % 12;
  hours = hours ? hours : 12; // the hour '0' should be '12'
  minutes = minutes < 10 ? '0'+minutes : minutes;
  var strTime = hours + ':' + minutes + ' ' + ampm;
  return strTime;
}


alert('Current Time: ' + formatAMPM(currentTime));

alert('Future Time: ' + formatAMPM(futureTime));

alert('Past Time: ' + formatAMPM(pastTime));

在这里试试: http://jsfiddle.net/atheos/Wx2vw/