获取当前文档的Last-Modified日期在JavaScript Date对象 [英] Get current document's Last-Modified date in javascript Date object

查看:362
本文介绍了获取当前文档的Last-Modified日期在JavaScript Date对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

浏览器提供了一种通过观察来确定文件的最后修改日期 document.lastModified 。这家酒店是从HTTP 的Last-Modified 标题来确定,并返回一个字符串。

The browser provides a way to determine a document's last-modified date by looking at document.lastModified. This property is determined from the HTTP Last-Modified header, and is returned as a string.

我的目标是将此属性转换为一个Javascript 日期对象。目前我使用

My goal is to convert this property to a Javascript Date object. Currently I am using

var date = new Date(document.lastModified);

成功地解析字符串。不过,我很好奇,这是否会跨浏览器和跨区域设置工作。

which successfully parses the string. However, I am curious as to whether this will work across browsers and across locales.

什么是对我来说很有意思的是, document.lastModified 重新presents相同的日期作为HTTP 的Last-Modified 头给出,但字符串是不相同的。在我看来,浏览器解析的Last-Modified 头,将其转换成其内部日期重新presentation,然后设置文件。上次更改来基于该字符串。如果是这种情况, document.lastModified 是可能的方式使得它可以被Javascript 日期的构造,因为它们都可能使用相同的语言环境和格式规则。但我一直无法证实这一点是肯定的。

What is very interesting to me is that the document.lastModified represents the same date as the HTTP Last-Modified header given, but the strings are not identical. It seems to me that the browser parses the Last-Modified header, converts it to its internal date representation, and then sets document.lastModified to a string based on that. If this is the case, document.lastModified is likely to be formatted in a way such that it can be parsed by the Javascript Date constructor, as they are both likely using the same locale and formatting rules. But I've been unable to confirm this for sure.

推荐答案

面对面的人livibetter的评论,现在,我认为,标准本身,每一个浏览器就可以为所欲为,因为我不能没有找到有关document.lastModified在HTML4,XHTML,DOM 2或3 DOM什么,我相信这只是在HTML5中定义的

Vis-á-vis livibetter's comment, "For now, I think, standard per se, every browser can do whatever they like because I can't not find anything about document.lastModified in HTML4, XHTML, DOM 2, or DOM 3. I believe it's only defined in HTML5"

我相信document.lastModified已经有一段时间了。现在我已经受够了在一个网站上了几年(前HTML5)。不,它真的很重要。如果有人关心(在Windows XP SP3)我保持周围的浏览器检查网页:

I believe "document.lastModified" has been around for a while. I've had it on one site for a few years now (before HTML5). Not that it really matters. If anyone cares (under Windows XP SP3) the browsers I keep around to check pages:

·34铬仍坚持在返回以UTC时间(但没有当地时间加)结果
·火狐28只忽略声明的javascript:警惕(document.lastModified)! :D结果
·IE8返回当地时(正如其他人指出的)结果
·歌剧12.16返回当地时间结果
·Safari浏览器5.1.7返回UTC时间结果

· Chrome 34 still insists in returning the time in UTC (but no local time addition)
· Firefox 28 just ignores the statement "javascript:alert(document.lastModified)"!! :D
· IE8 returned local time (as others have pointed out)
· Opera 12.16 returned local time
· Safari 5.1.7 returned UTC time

至少他们得到的日期的权利;他们都在我的主页回到09年11月17日(除火狐刚刚继续无视)! LOL

At least they're getting the date right; they all returned 11/17/09 on my home page(except Firefox which just continued to ignore)! LOL

这将是,如果参考来源好看,W3Schools的一样( http://www.w3schools.com /jsref/prop_doc_lastmodified.asp ),将沿着这些线路增加一些空话所以人知道会发生什么,当结果是时髦的。

It would be nice if reference sources, like W3Schools (http://www.w3schools.com/jsref/prop_doc_lastmodified.asp), would add some verbiage along these lines so folks know what to expect when the results are funky.

更新:如果您激活工具| Web开发| DOM查看器,火狐28高高兴兴地执行的javascript:警惕(document.lastModified),并返回本地时间!什么是大杂烩。 LOL

UPDATE: If you activate Tools | Web Developer | DOM Inspector, Firefox 28 happily executes "javascript:alert(document.lastModified)" and returns local time! What a mish-mash. LOL

这篇关于获取当前文档的Last-Modified日期在JavaScript Date对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆