lastModified()函数返回当前日期和时间

lastModified() function returns current date and time

本文关键字:当前日期 时间 返回 函数 lastModified      更新时间:2023-09-26

我的问题是:为什么当我在网页上使用"document.lastModified"时,它会返回当前日期和时间,而不是上次修改此页面的时间。。。有什么想法吗?提前感谢:(

实际代码为:

<script type="text/javascript"> document.write("Page was last modified on: " + document.lastModified);</script>

我今天做了一个实验。我把以下脚本放在我的服务器上的一个文件中,最后一次修改是在1月1日:


var theDate = new Date(document.lastModified).toISOString().substr(5, 5).replace('-', '');
document.write(theDate);

并请来访者告诉我他们看到了什么。103表示看到"0101",预期字符串,8表示看到"0308",即当前日期。所有糟糕的答案都来自安卓、Chrome/7[12]用户。好的答案(22(也来自其他安卓用户,通常使用早期版本的Chrome。然而,有10名安卓Chrome/72用户看到了这次美好的约会。

基于这个小样本,Android Chrome 71+版本肯定有问题,但这是随机的。

因为您当前正在修改它。例如,看看这个。

要根据您的要求进行此操作,请检查此链接和此链接

从外部链接复制:

把这个放在底部的页面上:

 <script type="text/javascript" src="js_lus.js"></script>

随意命名文件。示例:js_lus.js确保src="quot;路径对于您的所有页面都是正确的。

function lastModified() {
    var modiDate = new Date(document.lastModified);
    var showAs = modiDate.getDate() + "-" + (modiDate.getMonth() + 1) + "-" + modiDate.getFullYear();
    return showAs
}
function GetTime() {
    var modiDate = new Date();
    var Seconds
    if (modiDate.getSeconds() < 10) {
        Seconds = "0" + modiDate.getSeconds();
    } else {
        Seconds = modiDate.getSeconds();
    }
    var modiDate = new Date();
    var CurTime = modiDate.getHours() + ":" + modiDate.getMinutes() + ":" + Seconds
    return CurTime
}
document.write("Last updated on ")
document.write(lastModified() + " @ " + GetTime());
document.write(" [D M Y 24 Hour Clock]")
document.write("");

我在用PHP编程时遇到过这个问题。在我的案例中,问题是由于PHP是一种服务器端语言,所以每次调用页面时都会重新渲染页面,从而在编辑页面时重置所需的javascript代码。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<html><head><title>Last Modified Date</title></head><body>
Last modified:
<script language="JavaScript">
var testlast=document.lastModified;
document.write(" "+testlast.substr(0,10));
</script><br>
</body></html>

该代码应返回源文档的"最后修改日期"。过去总是这样。正在更改的"文档"是网页的显示形式,而不是来源。第3行应该是获取文档的修改日期,该日期在页面显示之前、期间或之后不会更改。因此"testlast"应该是源的修改日期,而不是当前日期。当我将源代码放在Macintosh上,并从"localhost"访问它时,此代码运行良好。