页面最后使用Javascript修改,12小时时钟显示上午或下午

时间:2016-11-23 21:24:14

标签: javascript time last-modified

我正在尝试在Javascript中创建一个脚本,该脚本显示上次修改页面的时间,该脚本返回修改日期,时间,上午或下午格式。

显然我做错了什么。我无法让脚本运行,它将在我的函数AmPm中。有人可以帮忙吗?

// Javascript code for page modification
// Shows the date, time, am or pm, of modification.

// This section sets the date of modification
function lastModified() {
  var modiDate = new Date(document.lastModified);
  var showAs = modiDate.getDate() + "." + (modiDate.getMonth() + 1) + "." + modiDate.getFullYear();
  return showAs
}

// This section sets the time of modification
function GetTime() {
  var modiDate = new Date();
  var Seconds

  if (modiDate.getSeconds() < 10) {
    Seconds = "0" + modiDate.getSeconds();
  } else {
    Seconds = modiDate.getSeconds();
  }

  // This section writes the above in the document
  var modiDate = new Date();
  var CurTime = modiDate.getHours() + ":" + modiDate.getMinutes() + ":" + Seconds
  return CurTime
}

// This section decides if its am or pm
function AmPm() {
  var hours = new Date().getHours();
  var hours = (hours + 24 - 2) % 24;
  var mid = 'AM';
  if (hours == 0) { // At 00 hours (midnight) we need to display 12 am
    hours = 12;
  } else if (hours > 12) // At 12pm (Midday) we need to display 12 pm
  {
    hours = hours % 12;
    mid = 'PM';
  }
}
var mid = //This is where I am stuck!!
  return AmPm

document.write("This webpage was last edited on: ");
document.write(lastModified() + " at " + GetTime() + AmPm());
document.write("");
document.write(" NZ Daylight Savings Time.");
document.write("");

1 个答案:

答案 0 :(得分:0)

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