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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16024346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 02:58:06  来源:igfitidea点击:

lastModified() function returns current date and time

javascript

提问by Eddi994

My question is : why when I use "document.lastModified" on my web page, it returns me the current date and time, not the time when this page was last Modified... Any ideas? Thanks in advance :)

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

Actual code is :

实际代码是:

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

采纳答案by karthikr

Because you are modifying it currently. Check this out for example.

因为您当前正在修改它。例如,检查一下。

To make this work based on your requirement, checkout this linkand this link

要根据您的要求进行这项工作,请查看此链接此链接

Copying from the external link:

从外部链接复制:

Put this on the page at the bottom:

把这个放在页面底部:

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

Name the file whatever you want. Example: js_lus.js Make sure src="" path is correct for all your pages.

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

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("");

回答by Dick Guertin

<!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>

That code should return the Last Modified Date of the source document. It always has in the past. The 'document' being changed is the display form of the web page, NOT THE SOURCE. Line 3 should be fetching the document's modification date, which does NOT change before, during, or after the page is displayed. So 'testlast' should be the source's modification date, NOT the current date. This code works fine when I place the source on my Macintosh, and access it from my 'localhost'.

该代码应返回源文档的上次修改日期。过去总是如此。正在更改的“文档”是网页的显示形式,而不是源。第 3 行应该获取文档的修改日期,该日期在页面显示之前、期间或之后不会更改。所以“testlast”应该是源的修改日期,而不是当前日期。当我将源代码放在我的 Macintosh 上并从我的“本地主机”访问它时,此代码工作正常。

回答by szigetva

I conducted an experiment today. I put the following script on my server in a file touched to be last modified on 1 Jan:

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


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

and asked visitors to tell me what they see. 103 said they see "0101", the expected string, 8 said they see "0308", ie the current date. All the bad answers came from Android, Chrome/7[12] users. Good answers (22) also came from other Android users, typically with earlier versions of Chrome. However there were 10 Android Chrome/72 users who saw the good date.

并要求游客告诉我他们看到了什么。103 表示他们看到“0101”,即预期字符串,8 表示他们看到“0308”,即当前日期。所有糟糕的答案都来自 Android、Chrome/7[12] 用户。好的答案 (22) 也来自其他 Android 用户,通常使用早期版本的 Chrome。然而,有 10 个 Android Chrome/72 用户看到了这个好日子。

Based on this small sample, there must be something wrong in Android Chrome versions 71+, but it is stochastic.

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