Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 函数的作用是:返回当前日期和时间_Javascript - Fatal编程技术网

Javascript 函数的作用是:返回当前日期和时间

Javascript 函数的作用是:返回当前日期和时间,javascript,Javascript,我的问题是:为什么当我在我的网页上使用“document.lastModified”时,它会返回我当前的日期和时间,而不是上次修改此网页的时间。。。 有什么想法吗? 提前感谢:) 实际代码为: <script type="text/javascript"> document.write("Page was last modified on: " + document.lastModified);</script> document.write(“上次修改页面的日期:“+

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

实际代码为:

<script type="text/javascript"> document.write("Page was last modified on: " + document.lastModified);</script>
document.write(“上次修改页面的日期:“+document.lastModified”);

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

根据您的要求完成这项工作,以及

从外部链接复制:

把这个放在页面底部:


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

函数lastModified(){
var modiDate=新日期(document.lastModified);
var showAs=modiDate.getDate()+“-”+(modiDate.getMonth()+1)+“-”+modiDate.getFullYear();
返回showAs
}
函数GetTime(){
var modiDate=新日期();
var秒
if(modiDate.getSeconds()<10){
Seconds=“0”+modiDate.getSeconds();
}否则{
秒=modiDate.getSeconds();
}
var modiDate=新日期();
var CurTime=modiDate.getHours()+“:”+modiDate.getMinutes()+“:”+Seconds
回程时间
}
文件。写入(“上次更新日期”)
write(lastModified()+“@”+GetTime());
文件。写(“[D M Y 24小时时钟]”)
文件。填写(“”);

上次修改日期
上次修改:
var testlast=document.lastModified;
document.write(“+testlast.substr(0,10));


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

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


var theDate = new Date(document.lastModified).toISOString().substr(5, 5).replace('-', '');
document.write(theDate);
让游客告诉我他们看到了什么。103表示他们看到了“0101”,即预期的字符串,8表示他们看到了“0308”,即当前日期。所有糟糕的答案都来自Android、Chrome/7[12]用户。好答案(22)也来自其他Android用户,通常是早期版本的Chrome。然而,有10名Android Chrome/72用户看到了这一美好的日子


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

我在使用PHP编程时遇到了这个问题。在我的例子中,问题是由于PHP是一种服务器端语言,因此每次调用该页面时都会重新呈现该页面,从而在编辑该页面时重置请求的javascript代码。

因为您当前正在修改它:)例如,请检查:如果您使用的是服务器端脚本语言,文档将始终在加载页面时进行最后一次修改。此外,并非所有服务器都会正确返回此内容。如果答案(如您所链接的)简短而甜蜜(如您所链接的),则您应该真正在答案中输入实际的解决方案/代码,然后对链接给予信任/引用。这样,如果链接每次都消失,答案仍然在这里可用。那么为什么在URL栏中粘贴
javascript:alert(document.lastModified)
也会返回当前时间?这肯定不会修改文档吗?
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("");
<!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>

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