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

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

问题描述

浏览器提供了确定文档上次更改日期的方法通过查看 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 Date 对象。目前我正在使用

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 表示与给定的HTTP Last-Modified 头相同的日期,但字符串不相同。在我看来,浏览器解析 Last-Modified 标头,将其转换为内部日期表示,然后设置 document.lastModified 到一个基于此的字符串。如果是这种情况,那么 document.lastModified 可能会以某种方式进行格式化,以便可以通过Javascript解析 Date 构造函数,因为它们都可能使用相同的区域设置和格式化规则。但是我一直无法确认这一点。

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.

推荐答案

Vis-á-vis livibetter的评论:现在,我认为,标准本身,每个浏览器都可以做任何他们喜欢的,因为我无法找到任何关于document.lastModified在HTML4,XHTML,DOM 2或DOM 3中的任何内容。我相信它只是在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:

·Chrome 34仍然坚持以UTC(UTC)返回时间)

·Firefox 28只是忽略声明javascript:alert(document.lastModified)! :D

·IE8返回当地时间(其他人已经指出)

·Opera 12.16返回本地时间

·Safari 5.1.7返回UTC时间< br>

· 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

至少他们得到日期正确;他们都在11/17/09返回我的主页(除了Firefox,只是继续忽略)! 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.

更新:如果激活工具|网页开发者| DOM Inspector,Firefox 28快乐地执行javascript:alert(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

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

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