简介

实时获取当前时间并展示在页面上,是很多地方常用的;在页面中做此展示以便查看时间。本文使用js通过一个小的demo实现效果。

完整Code

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>实时展示当前时间</title>
</head>
<style>
    #time {
        width: 60%;
        padding: 10px;
        border-radius: 6px;
        background-color: lightcoral;
        color: white;
    }

    #time1 {
        width: 60%;
        padding: 10px;
        border-radius: 6px;
        background-color: lightcoral;
        color: white;
        margin-top: 30px;
    }
</style>

<body>
    <div id="time"></div>
    <div id="time1"></div>
</body>
<script>
    /**
     * 简介:
     * 下面用js简单的实现 在页面中实时展示当前的时间; 
     * 
     */
    let time = new Date();
    setInterval(() => {
        time = new Date();
        document.getElementById('time').innerHTML = `标准时间:${time}`; // 标准时间:Wed Apr 22 2020 18:53:43 GMT+0800 (中国标准时间)
        document.getElementById('time1').innerHTML = `转化后的时间:${time.toLocaleDateString()} ${time.toLocaleTimeString()}`; // 转化后的时间:2020/4/22 下午6:54:25
    }, 1000)
    document.getElementById('time').innerHTML = `标准时间:${time}`;
    document.getElementById('time1').innerHTML = `转化后的时间:${time.toLocaleDateString()} ${time.toLocaleTimeString()}`
    console.log(new Date()); // Wed Apr 22 2020 18:53:21 GMT+0800 (中国标准时间)
</script>

</html>

展示效果

在这里插入图片描述

介绍

技术点

  1. 定时器;
  2. 获取当前时间;
  3. 时间转换格式;
  4. 获取元素且展示;
  5. ES6;

后续未完,请继续关注,Thanks!☺


Sun_jiejie
24 声望2 粉丝

« 上一篇
JS的test()方法