admin管理员组

文章数量:1400007

I want to show the last updated (modified) time in my website. How can I do it in html. This is my script

<!DOCTYPE html>
<html>
<body>
This is last updated time

<body onload="myFunction()">
<script>
function myFunction() {
  var x = document.lastModified;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

I want to show the last updated (modified) time in my website. How can I do it in html. This is my script

<!DOCTYPE html>
<html>
<body>
This is last updated time

<body onload="myFunction()">
<script>
function myFunction() {
  var x = document.lastModified;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

Share Improve this question asked Sep 9, 2019 at 16:05 KimHeeKimHee 7883 gold badges13 silver badges25 bronze badges 3
  • It's unclear if you are asking how to do this without JS or if you want to know why this code isn't working (in which case the error message in the content should be a big clue). What element are you trying to get by its ID? – Quentin Commented Sep 9, 2019 at 16:08
  • It also appears, that document.lastModified returns only the current date and time when retrieved on a web page. – Teemu Commented Sep 9, 2019 at 16:13
  • Your html is not well formed, so your question is really weird. – iguypouf Commented Sep 9, 2019 at 16:13
Add a ment  | 

3 Answers 3

Reset to default 4

You can use the date object and then use ISOString method,the use substr to get the time from the string

function myFunction() {
  var x = document.lastModified;
  document.getElementById("demo").innerHTML = new Date(document.lastModified).toISOString().substr(11, 8);
}

myFunction()
This is last updated time <span id='demo'></span>

First you have 2 bodys, dont want that.. then you just need to add the #demo element to select and innerHTML the lastModified value.

<!DOCTYPE html>
<html>

<body onload="myFunction()">
  This is last updated time <span id="demo"></span>
  
  <script>
    function myFunction() {
      var x = document.lastModified;
      document.getElementById("demo").innerHTML = x;
    }
  </script>
</body>

</html>

As Requested, All you needed was a div element with id "demo"

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script>
        function loadTime() {
            document.getElementById("demo").innerHTML = document.lastModified;
        }
    </script>
</head>
<body onload="loadTime();">
    <div id="demo"></div>
</body>
</html>

本文标签: javascriptHow to display the last update time in htmlStack Overflow