整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:

HTML网络编程之时间格式

HTML网络编程之时间格式

络编程之时间格式。

同学们好,今天我们分享的是如何让搜索引擎等程序更容易地提取网页中的时间信息。我们将使用time标签来实现这一目标。这个标签你们可能已经有所了解,但是现在不需要掌握太多细节,只需要知道它的作用即可。

现在来看看我们的示例页面,可以看到页面中包含了很多句不同时间格式的文字。这些文字并没有什么特别之处,只是每一句都包含着时间信息。时间信息的格式比较复杂,但是这不影响我们的演示效果。

接下来,我们将介绍实现代码。time标签用于定义公历日期或时间、二十四小时制,时间和时区偏移是可选的。在所有浏览器中,time标签不会渲染任何特殊的效果。但是,它可以让搜索引擎更容易地在网页中找到对应的时间信息。

使用time标签的另一个原因是,世界上有许多不同的日期格式,但是这些不同的格式不容易被电脑识别。如果我们想自动抓取页面上所有事件的日期并将它们插入到日历中,time元素可以让我们附上清晰的可被机器识别的时间或日期。因此,time标签并不是为了给用户看的,而是为了方便搜索引擎更好地在网页上找到对应的时间。

在我们的示例中,时间和普通文字看上去没有任何区别。除了搜索引擎,网页同手机上的日历、提醒等应用程序交互时,time标签也可以提供很大的方便。

time标签非常简单,只包含一个属性datatime,用于规定日期和时间。如果需要,我们还可以通过元素的内容来指定日期和时间。time标签的值有很多种,只要是符合规范的时间写法格式,都可以被接受并转化为第三方使用的格式。

总之,time标签的使用频率并不高,不需要我们进行太多的学习和理解。如果你们知道有这个东西并且知道它的大概意思,就可以了。

今天的分享就到这里,所有的案例和相关文档都可以向我索取。

下期见,想学习编程的同学请关注我。

ue 示例

在JavaScript或Vue中获取当前时间并格式化输出到精确的时分秒,你可以使用Date对象结合字符串拼接或者模板字符串来实现。下面是一个简单示例,展示了如何在Vue中完成这项任务:

<template>
  <div>
    <p>当前时间:{{ formattedTime }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      formattedTime: ''
    };
  },
  mounted() {
    this.updateTime();
    setInterval(this.updateTime, 1000); // 每秒更新一次
  },
  methods: {
    updateTime() {
      const now=new Date();
      // 使用模板字符串进行格式化
      this.formattedTime=`${now.getFullYear()}-${this.padZero(now.getMonth() + 1)}-${this.padZero(now.getDate())} ${this.padZero(now.getHours())}:${this.padZero(now.getMinutes())}:${this.padZero(now.getSeconds())}`;
    },
    // 辅助函数,用于补零操作
    padZero(num) {
      return num < 10 ? '0' + num : num;
    }
  },
  beforeDestroy() {
    // 清除定时器,避免内存泄漏
    clearInterval(this.timer);
  }
};
</script>

在这个示例中,我们在Vue组件的mounted生命周期钩子中初始化了一个定时器,每秒钟调用updateTime方法来更新当前时间,并在组件销毁前通过beforeDestroy钩子清理定时器。

updateTime方法中,我们创建了一个新的Date对象来获取当前时间,然后使用模板字符串和辅助函数padZero来确保月份、日期、小时、分钟和秒数如果是个位数,则在其前补零,以便格式统一和美观。

这样,页面上就会显示一个实时更新的当前时间,格式为“年-月-日 时:分:秒”。

Vue3 Composition API 示例

在Vue3中,虽然一些API和写法有所变化,但获取和格式化当前时间的基本逻辑与Vue2相似。以下是使用Vue3 Composition API的一个示例:

<template>
  <div>
    <p>当前时间:{{ formattedTime }}</p>
  </div>
</template>

<script setup>
import { ref, onMounted, onBeforeUnmount } from 'vue';

const formattedTime=ref('');
let timer=null;

const updateTime=()=> {
  const now=new Date();
  formattedTime.value=`${now.getFullYear()}-${padZero(now.getMonth() + 1)}-${padZero(now.getDate())} ${padZero(now.getHours())}:${padZero(now.getMinutes())}:${padZero(now.getSeconds())}`;
};

const padZero=(num)=> {
  return num < 10 ? '0' + num : num;
};

onMounted(()=> {
  updateTime();
  timer=setInterval(updateTime, 1000); // 每秒更新一次
});

onBeforeUnmount(()=> {
  // 清除定时器
  clearInterval(timer);
});
</script>

在这个Vue3的示例中,我们使用了Composition API来管理状态和生命周期钩子。ref用于定义响应式数据formattedTime,而onMounted和onBeforeUnmount分别替代了Vue2中的mounted和beforeDestroy生命周期钩子。

updateTime函数和padZero辅助函数的功能与Vue2示例相同,用于获取当前时间并进行格式化处理,以及在数字小于10时前面添加零。

这样,你就可以在Vue3应用中实现实时更新并格式化显示当前时间的功能。

TypeScript 公共函数封装

使用TypeScript可以为你的代码增加类型安全。下面是如何封装一个获取并格式化当前时间的公共函数,这个函数可以在Vue3的项目中作为公共方法使用。

首先,在你的Vue3项目的某个公用文件夹(如src/utils)下创建一个名为dateTimeUtils.ts的文件,并编写如下代码:

// src/utils/dateTimeUtils.ts

export function formatCurrentTime(): string {
  const now=new Date();
  return `${now.getFullYear()}-${padZero(now.getMonth() + 1)}-${padZero(now.getDate())} ${padZero(now.getHours())}:${padZero(now.getMinutes())}:${padZero(now.getSeconds())}`;
}

function padZero(num: number): string {
  return num < 10 ? '0' + num : num.toString();
}

这个模块导出了一个formatCurrentTime函数,它返回当前时间的格式化字符串。同时,内部使用了padZero辅助函数来保证数字的格式正确。

然后,在你的Vue3组件中,你可以这样使用这个公共函数:

// 某个Vue3组件.vue文件

<script setup lang="ts">
import { onMounted, ref } from 'vue';
import { formatCurrentTime } from '@/utils/dateTimeUtils'; // 根据你的实际路径调整

const formattedTime=ref('');

onMounted(()=> {
  formattedTime.value=formatCurrentTime();
  setInterval(()=> {
    formattedTime.value=formatCurrentTime();
  }, 1000);
});
</script>

<template>
  <div>
    <p>当前时间:{{ formattedTime }}</p>
  </div>
</template>

这里,我们导入了formatCurrentTime函数,并在组件挂载时设置初始值,之后每秒更新一次显示的时间。注意,为了避免潜在的内存泄漏,如果组件需要销毁时停止时间更新,你可能还需要在适当的生命周期钩子中清除定时器,正如之前Vue2和Vue3 Composition API示例中所示。不过,在此示例中为了保持简洁,省略了该部分代码。

家我呀,我是yangyang,此刻看到一个小特效,是国外一哥们做的,特来分享给大家.

效果介绍

数字时代,数字发光时钟体现了形式与功能的融合。在这篇文章中,我们将深入研究如何使用 HTML、CSS 和 JavaScript 来构建一个。

HTML

构建一个结构化基础,其中包含小时、分钟、秒和可选的 AM/PM 指示器元素。可访问性至关重要,因此我们将使用语义标签并提供有意义的描述。

<!DOCTYPE html>
<html lang="zh-cn">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Digital clock</title>
    <link rel="stylesheet" href="css/style.css" />
  </head>
  <body>
    <div class="hero">
      <div class="box">
        <style></style>
        <div class="clock">
          <span id="hrs">00</span>
          <span>:</span>
          <span id="min">00</span>
          <span>:</span>
          <span id="sec">00</span>
        </div>
      </div>
    </div>
    <script src="js/index.js"></script>
  </body>
</html>

css

利用 box-shadow 和 text-shadow 等属性,我们将为时钟注入活力。通过微调颜色和发光强度,我们将确保它吸引用户。

* {
  margin: 0;
  padding: 0;
  font-family: "Poppins", sans-serif;
  box-sizing: border-box;
}
.hero {
  width: 100%;
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #2f363e;
  position: relative;
}
.box {
  position: relative;
  width: 800px;
  height: 300px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.clock {
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(40px);
  color: #6e7f92f6;
  z-index: 10;
}
.clock span {
  font-size: 80px;
  width: 110px;
  display: inline-block;
  text-align: center;
  position: relative;
}
.clock span::after {
  font-size: 16px;
  position: absolute;
  bottom: -15px;
  left: 50%;
  transform: translateX(-50%);
}
#hrs::after {
  content: "HOURES";
  color: #0f0;
  font-weight: 600;
  margin-bottom: -10px;
}
#min::after {
  content: "MINS";
  color: #0ff;
  font-weight: 600;
  margin-bottom: -10px;
}
#sec::after {
  content: "SEC";
  color: #ff0;
  font-weight: 600;
  margin-bottom: -10px;
}

/*------Anemated Border---------*/
.box::before {
  content: "";
  position: absolute;
  inset: 0;
  background: repeating-conic-gradient(
    from var(--a),
    #0f0,
    #ff0,
    #0ff,
    #f0f,
    #0ff
  );
  border-radius: 20px;
  animation: rotate 6s linear infinite;
}
.box::after {
  content: "";
  position: absolute;
  inset: 0;
  background: repeating-conic-gradient(
    from var(--a),
    #0f0,
    #ff0,
    #0ff,
    #f0f,
    #0ff
  );
  border-radius: 20px;
  animation: rotate 4s linear infinite;
  filter: blur(40px);
  opacity: 0.75;
}
.box style {
  position: absolute;
  inset: 4px;
  background: #2f363e;
  border-radius: 16px;
  color: #ff0;
  font-size: 20px;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}
@property --a {
  syntax: "<angle>";
  inherits: false;
  initial-value: 0deg;
}
@keyframes rotate {
  0% {
    --a: 0;
  }
  0% {
    --a: -360deg;
  }
}

js

JavaScript 为我们的时钟注入了活力,实现了小时、分钟和秒的实时更新。我们还将考虑添加时区支持和用户偏好的自定义选项等功能。