<!doctype html> <html> <head> <meta charset="utf-8"> <title>获得文件的最后修改时间</title> <style> body{ background: yellow; } .center{ position: absolute; top:50%; left: 50%; transform:translate(-50%,-50%); width: 300px; height: 110px; border: 2px solid red; padding: 10px; } p{ text-align: center; margin: 0px; padding: 10px; } </style> </head> <body> <script> //获得文件的格式化最后修改时间 function DocDate() { var StrofLastMod = document.lastModified; //获得文件的最后修改时间,它是一个string类型的变量。 var months = ["January","February","March","April","May","June","July","August", "September","October","November","December"]; //月份名变量,供转换。 var days = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];//星期名变量,供转换 var c = ":"; //用来分隔时,分,秒。 var mdate =new Date(StrofLastMod); var month = months[mdate.getMonth()]; var date = mdate.getDate(); var year = mdate.getFullYear(); var day = days[mdate.getDay()]; var minutes=mdate.getMinutes(); var hours= mdate.getHours(); var seconds=mdate.getSeconds(); var miliSec=mdate.getMilliseconds(); var dateStr = year+" "+month+" "+date+" "+day+" "+hours+c+minutes+c+seconds+"."+miliSec; //获得格式化的文件最后修改时间。 return dateStr; } var div=document.createElement("div"); div.className="center"; var divson=document.createElement("div"); divson.innerHTML="<p>This File is last updated at:</p><p>"+DocDate()+"</p><p>"+document.lastModified+"</p>"; div.appendChild(divson); document.body.appendChild(div); </script> </body> </html>
PS:这里再为大家推荐几款比较实用的天数计算在线工具供大家使用: