-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateDifferenceCalculator.html
More file actions
359 lines (307 loc) · 10.4 KB
/
Copy pathDateDifferenceCalculator.html
File metadata and controls
359 lines (307 loc) · 10.4 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>日期差值计算器 - JustHTML 只需网页</title>
<script>
/**
* 动态加载(强制去除缓存) CSS 的函数
* @param {string} src - 要加载的 CSS 的 src 链接
* @returns {Promise} 返回一个 Promise 对象,用于处理异步加载
*/
function loadStyle(src) {
return new Promise((resolve, reject) => {
const link = document.createElement('link');
link.rel = 'stylesheet';
link.href = src + '?v=' + new Date().getTime(); // 使用时间戳避免缓存
link.async = true;
link.onload = resolve;
link.onerror = reject;
document.head.appendChild(link);
});
}
/**
* 动态加载(强制去除缓存) JS 的函数
* @param {string} src - 要加载的 JS 的 src 链接
* @returns {Promise} 返回一个 Promise 对象,用于处理异步加载
*/
function loadScript(src) {
return new Promise((resolve, reject) => {
const script = document.createElement('script');
script.src = src + '?v=' + new Date().getTime(); // 使用时间戳避免缓存
script.async = true;
script.onload = resolve;
script.onerror = reject;
document.head.appendChild(script);
});
}
</script>
<script>
loadStyle('../assets/style.css')
let scriptArray = [
"../assets/AddHeader_Tools.js",
"../assets/AddFooter.js",
"../assets/AddIco.js",
"../assets/AutoRun.js",
]
scriptArray.forEach(script => {
loadScript(script);
});
</script>
<script src="../package/Vue/code/vue.global.prod.js"></script>
<script type="module" src="../package/Sober/code/main.js"></script>
<style>
.date-container {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 20px;
margin: 20px 0;
}
.date-section {
display: flex;
flex-direction: column;
}
.date-section input {
padding: 8px;
margin-top: 5px;
}
.controls {
display: flex;
flex-wrap: wrap;
gap: 10px;
margin: 15px 0;
}
.info-box {
background-color: var(--bg-tertiary);
padding: 10px;
border-radius: var(--border-radius);
margin: 10px 0;
}
.result-container {
margin-top: 20px;
padding: 15px;
background-color: var(--bg-tertiary);
border-radius: var(--border-radius);
}
.result-item {
margin: 10px 0;
padding: 10px;
background-color: var(--bg-secondary);
border-radius: var(--border-radius);
}
.result-label {
color: var(--accent-color);
font-weight: bold;
}
.examples {
margin-top: 20px;
padding: 15px;
background-color: var(--bg-tertiary);
border-radius: var(--border-radius);
}
.example-item {
margin: 8px 0;
padding: 5px;
background-color: var(--bg-secondary);
border-radius: 4px;
}
@media (max-width: 768px) {
.date-container {
grid-template-columns: 1fr;
}
}
</style>
</head>
<body>
<div id="app">
<div class="container">
<a href="../index.html" class="back-link">← 返回首页</a>
<div class="tool-section">
<h2>日期差值计算器</h2>
<p>计算两个日期之间的差值</p>
<div class="info-box">
<p><strong>功能说明:</strong></p>
<p>• 计算两个日期之间的天数、小时数、分钟数等</p>
<p>• 支持选择日期和时间</p>
<p>• 结果包含多种时间单位的表示</p>
</div>
<div>
<div class="date-container">
<div class="date-section">
<label for="startDate">开始日期</label>
<input
type="datetime-local"
id="startDate"
v-model="startDate"
>
<label for="endDate">结束日期</label>
<input
type="datetime-local"
id="endDate"
v-model="endDate"
>
</div>
<div class="date-section">
<label>参考信息</label>
<div class="result" style="padding: 10px; margin-top: 5px;">
<p>开始: {{ startDate ? new Date(startDate).toLocaleString() : '未选择' }}</p>
<p>结束: {{ endDate ? new Date(endDate).toLocaleString() : '未选择' }}</p>
</div>
</div>
</div>
<div class="controls">
<button @click="calculateDiff">计算差值</button>
<button @click="setTodayAsStart">设为今天(开始)</button>
<button @click="setTodayAsEnd">设为今天(结束)</button>
<button @click="clearDates">清空日期</button>
</div>
<div class="result-container" v-if="results">
<h3>计算结果</h3>
<div class="result-item">
<span class="result-label">总差值:</span> {{ results.totalDays }} 天 {{ results.totalHours % 24 }} 小时 {{ results.totalMinutes % 60 }} 分钟
</div>
<div class="result-item">
<span class="result-label">相差年数:</span> {{ results.years }} 年
</div>
<div class="result-item">
<span class="result-label">相差月数:</span> {{ results.months }} 个月
</div>
<div class="result-item">
<span class="result-label">相差周数:</span> {{ results.weeks }} 周
</div>
<div class="result-item">
<span class="result-label">相差天数:</span> {{ results.days }} 天
</div>
<div class="result-item">
<span class="result-label">相差小时:</span> {{ results.hours }} 小时
</div>
<div class="result-item">
<span class="result-label">相差分钟:</span> {{ results.minutes }} 分钟
</div>
<div class="result-item">
<span class="result-label">相差秒数:</span> {{ results.seconds }} 秒
</div>
<div class="result-item">
<span class="result-label">工作日数:</span> {{ results.workdays }} 天
</div>
</div>
<div class="examples">
<h3>使用示例</h3>
<div class="example-item">计算项目持续时间</div>
<div class="example-item">计算生日距离今天的天数</div>
<div class="example-item">计算合同到期剩余时间</div>
<div class="example-item">计算年龄(年月日)</div>
</div>
</div>
</div>
</div>
</div>
<script>
const { createApp, ref, reactive } = Vue;
createApp({
setup() {
const startDate = ref('');
const endDate = ref('');
const results = ref(null);
// 计算日期差值
const calculateDiff = () => {
if (!startDate.value || !endDate.value) {
alert('请先选择开始日期和结束日期');
return;
}
const start = new Date(startDate.value);
const end = new Date(endDate.value);
if (isNaN(start.getTime()) || isNaN(end.getTime())) {
alert('日期格式无效');
return;
}
// 确保结束日期不早于开始日期
if (end < start) {
const temp = start;
start = end;
end = temp;
// 更新UI中的日期
const tempDate = startDate.value;
startDate.value = endDate.value;
endDate.value = tempDate;
}
// 计算总差值(毫秒)
const diffMs = end - start;
// 计算各时间单位
const diffSecs = Math.floor(diffMs / 1000);
const diffMins = Math.floor(diffSecs / 60);
const diffHours = Math.floor(diffMins / 60);
const diffDays = Math.floor(diffHours / 24);
// 计算年月日
let years = end.getFullYear() - start.getFullYear();
let months = end.getMonth() - start.getMonth();
let days = end.getDate() - start.getDate();
if (days < 0) {
months--;
const prevMonth = new Date(end.getFullYear(), end.getMonth(), 0);
days += prevMonth.getDate();
}
if (months < 0) {
years--;
months += 12;
}
// 计算工作日(周一到周五)
let workdays = 0;
const tempDate = new Date(start);
tempDate.setHours(0, 0, 0, 0);
const endTemp = new Date(end);
endTemp.setHours(0, 0, 0, 0);
while (tempDate <= endTemp) {
const dayOfWeek = tempDate.getDay();
if (dayOfWeek !== 0 && dayOfWeek !== 6) { // 不是周末
workdays++;
}
tempDate.setDate(tempDate.getDate() + 1);
}
// 计算周数
const weeks = Math.floor(diffDays / 7);
results.value = {
years: years,
months: months,
weeks: weeks,
days: diffDays,
hours: diffHours,
minutes: diffMins,
seconds: diffSecs,
totalDays: diffDays,
totalHours: diffHours,
totalMinutes: diffMins,
workdays: workdays
};
};
// 设为今天(开始)
const setTodayAsStart = () => {
const now = new Date();
startDate.value = now.toISOString().slice(0, 16);
};
// 设为今天(结束)
const setTodayAsEnd = () => {
const now = new Date();
endDate.value = now.toISOString().slice(0, 16);
};
// 清空日期
const clearDates = () => {
startDate.value = '';
endDate.value = '';
results.value = null;
};
return {
startDate,
endDate,
results,
calculateDiff,
setTodayAsStart,
setTodayAsEnd,
clearDates
};
}
}).mount('#app');
</script>
</body>
</html>