js封装 JS 时间格式化函数

将当前时间戳转换为日期格式(年-月-日-时-分-秒)

function formatDate() {
  const date = new Date();
  const year = date.getFullYear();
  const month = ('0' + (date.getMonth() + 1)).slice(-2);
  const day = ('0' + date.getDate()).slice(-2);
  const hours = ('0' + date.getHours()).slice(-2);
  const minutes = ('0' + date.getMinutes()).slice(-2);
  const seconds = ('0' + date.getSeconds()).slice(-2);
  return `${year}-${month}-${day}-${hours}-${minutes}-${seconds}`;
}

使用方法:

const formattedDate = formatDate(); // '2022-01-01-13-00-00'(示例)
console.log(formattedDate);

在 ECharts 的 X 轴上显示当前日期前一周的月/日

// 在 ECharts 的 X 轴上显示当前日期前一周的月日
        const today = new Date() // 当前日期
        const lastWeek = new Date(
          today.getFullYear(),
          today.getMonth(),
          today.getDate() - 6
        ) // 最近一周的日期
        const xAxisData = [] // 存储要显示的日期字符串
        for (let i = lastWeek.getTime(); i <= today.getTime(); i += 86400000) {
          const date = new Date(i)
          xAxisData.push(
            date.toLocaleDateString('en-US', {
              month: 'numeric',
              day: 'numeric',
            })
          )
        }

使用方法:

  xAxis: {
    type: 'category',
    data: xAxisData,
    axisLabel: {
      formatter: '{value}'
    }
  },

 在vue中获取当前时间动态显示在页面上

 在这个 Vue 组件中,我们使用了 Created 生命周期钩子来在组件创建时开始定时更新时间。使用 setInterval 在每秒钟的时间间隔内调用 updateTime 方法来更新当前时间。在 updateTime 方法中,我们使用类似原生 JavaScript 的方式来获取当前时间,并设置 Vue 实例中的 currentTime 数据。

最后,在模板中使用双括号绑定 currentTime 数据展示出当前时间。

<template>
  <div>{{ currentTime }}</div>
</template>

<script>
  export default {
    data() {
      return {
        currentTime: ''
      };
    },
    created() {
      this.updateTime();
      setInterval(() => {
        this.updateTime();
      }, 1000);
    },
    methods: {
      updateTime() {
        const now = new Date();
        const year = now.getFullYear();
        const month = ('0' + (now.getMonth() + 1)).slice(-2); // 月份从 0 开始
        const date = ('0' + now.getDate()).slice(-2);
        const hours = ('0' + now.getHours()).slice(-2);
        const minutes = ('0' + now.getMinutes()).slice(-2);
        const seconds = ('0' + now.getSeconds()).slice(-2);

        this.currentTime = `${year}-${month}-${date} ${hours}:${minutes}:${seconds}`;
      }
    }
  };
</script>

获取最近七个月时间数组

      getRecentSevenMonths() {
        const today = new Date() // 获取当前日期
        const months = [] // 用于存储近七个月的日期字符串
        // 循环生成近七个月的日期
        for (let i = 0; i < 7; i++) {
          const year = today.getFullYear()
          const month = today.getMonth() + 1
          const monthStr = month < 10 ? `0${month}` : `${month}`
          const dateStr = `${year}/${monthStr}`
          months.unshift(dateStr) // 将日期字符串添加到数组中
          today.setMonth(today.getMonth() - 1) // 递减一个月
        }
        this.months = months  
      },

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秋绥冬禧.

一键三联就是最大的支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值