要在静态网页中显示访问时间,你可以使用简单的 JavaScript 来获取当前日期和时间,并将其显示在网页上。下面是一个基本的示例代码,展示了如何实现这一点:
html
您访问的时间是:
// 获取当前时间
const now = new Date();
// 格式化时间
const options = {
year: 'numeric',
month: 'long',
day: 'numeric',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: false
};
// 将时间格式化为字符串
const formattedTime = now.toLocaleString('zh-CN', options);
// 显示在网页上
document.getElementById('visit-time').textContent = formattedTime;
在这个示例代码中:
1. 我们创建了一个基本的 HTML 结构。
2. 使用 JavaScript 的 `Date` 对象获取当前的日期和时间。
3. 使用 `toLocaleString` 方法将日期格式化为中文格式。
4. 最后,我们将格式化的时间插入到网页中。
只需将上述代码复制到一个 `.html` 文件中并在浏览器中打开,即可看到显示的访问时间。
查看详情
查看详情