0

I'm trying to create a script in Javascript that shows when a page was last modified, which returns the date, time, in am or pm format, of modification.

Clearly I am doing something wrong. I can't get the script to run, and it will be in my function AmPm. Can someone please help?

// 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("");
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Glenn
  • 1
  • 2
    if you want to save yourself the trouble, use moment.js – blurfus Nov 23 '16 at 21:25
  • Read up on what `document.write` does – j08691 Nov 23 '16 at 21:27
  • @j08691 as long as it is inline document.write is ok to use – mplungjan Nov 23 '16 at 21:30
  • @mplungjan never said it wasn't OK, just said they should read up on it to know how it works. Many new coders fail to understand it. – j08691 Nov 23 '16 at 21:31
  • Creating a snippet for you immediately gives console errors. Please fix that – mplungjan Nov 23 '16 at 21:31
  • 1
    Possible duplicate of [Converting 24 hour time to 12 hour time w/ AM & PM using Javascript](http://stackoverflow.com/questions/4898574/converting-24-hour-time-to-12-hour-time-w-am-pm-using-javascript) – Jason Yost Nov 23 '16 at 21:36
  • Just change to `mid = 'PM'; } return hours+mid; } document.write("This webpage was last edited on: "); document.write(lastModified() + " at " + AmPm());` – mplungjan Nov 23 '16 at 21:36

1 Answers1

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;
}
ZCoder
  • 2,155
  • 5
  • 25
  • 62