aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/file/lastmodified/index.html
blob: 3f0a31de7cb338d7e3fce311993fc02fbfc06181 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
---
title: File.lastModified
slug: Web/API/File/lastModified
tags:
  - API
  - File API
  - Files
translation_of: Web/API/File/lastModified
---
<p>{{APIRef("File API")}}</p>

<p>只读属性 <code><strong>File.lastModified</strong></code> 返回所引用文件最后修改日期, 为自 1970年1月1日0:00 以来的毫秒数。没有已知的最后修改时间则会返回当前时间。</p>

<h2 id="语法">语法</h2>

<pre class="brush:js">var time = <em>instanceOfFile</em>.lastModified;
</pre>

<h2 id="值"></h2>

<p>自 1970年1月1日0:00 以来的毫秒数。</p>

<h2 id="实例">实例</h2>

<h3 id="从INPUT标签读取文件">从INPUT标签读取文件</h3>

<pre class="brush: html">&lt;input type="file" multiple id="fileInput"&gt;
</pre>

<pre class="brush:js">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 &lt; files.length; i++) {
    const date = new Date(files[i].lastModified);
    alert(files[i].name + ' has a last modified date of ' + date);
  }
});
</pre>

<p>结果:</p>

<p>{{ EmbedLiveSample('Reading_from_file_input', 300, 50) }}</p>

<h3 id="动态创建文件">动态创建文件</h3>

<p>如果文件是动态创建的,可以在构造函数{{domxref("File.File()", "new File()")}} 中提供最后修改时间。如果未提供则会继承文件对象被创建时的{{jsxref("Date.now()")}}</p>

<pre class="brush:js">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
</pre>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('File API', '#file-attrs', 'lastModified')}}</td>
   <td>{{Spec2('File API')}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Edge</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari (WebKit)</th>
  </tr>
  <tr>
   <td>File.lastModified</td>
   <td>13.0</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoDesktop("15.0")}}</td>
   <td>10.0</td>
   <td>16.0</td>
   <td>{{CompatNo}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Edge</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Phone</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>File.lastModified</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="参见">参见</h2>

<ul>
 <li>{{domxref("File")}}</li>
</ul>