File.lastModified
只读属性 File.lastModified
返回所引用文件最后修改日期, 为自 1970年1月1日0:00 以来的毫秒数。没有已知的最后修改时间则会返回当前时间。
语法
var time = instanceOfFile.lastModified;
值
自 1970年1月1日0:00 以来的毫秒数。
实例
从INPUT标签读取文件
<input type="file" multiple id="fileInput">
const fileInput = document.getElementById('fileInput');
fileInput.addEventListener('change', function(event) {
// files is a FileList object (simliar to NodeList)
const files = event.target.files;
for (let i = 0; i < files.length; i++) {
const date = new Date(files[i].lastModified);
alert(files[i].name + ' has a last modified date of ' + date);
}
});
结果:
动态创建文件
如果文件是动态创建的,可以在构造函数new File()
中提供最后修改时间。如果未提供则会继承文件对象被创建时的Date.now()
。
var fileWithDate = new File([], 'file.bin', {
lastModified: new Date(2017, 1, 1),
});
console.log(fileWithDate.lastModified); //returns 1485903600000
var fileWithoutDate = new File([], 'file.bin');
console.log(fileWithoutDate.lastModified); //returns current time
规范
Specification | Status | Comment |
---|---|---|
File API lastModified |
Working Draft | Initial definition. |
浏览器兼容性
We're converting our compatibility data into a machine-readable JSON format.
This compatibility table still uses the old format,
because we haven't yet converted the data it contains.
Find out how you can help! (en-US)
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|---|
File.lastModified | 13.0 | (Yes) | 15.0 (15.0) | 10.0 | 16.0 | 未实现 |
Feature | Android | Edge | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
File.lastModified | 未实现 | (Yes) | 未实现 | 未实现 | 未实现 | 未实现 |