【vue 项目】格式化时间、获取当前时间、一小时前时间、一天前时间

接口请求参数需要传递时间
下面有三种方式获取当前时间

第一种

格式是最常见,采用三元判断添加0

  created() {
    
    
    this.getCurrentTime()
  },
  methods: {
    
    
    getCurrentTime() {
    
    
      // 获取当前时间并打印
      var _this = this
      const yy = new Date().getFullYear()
      const mm = new Date().getMonth() + 1 < 10 ? '0' + new Date().getMonth() : new Date().getMonth()
      const dd = new Date().getDate() < 10 ? '0' + new Date().getDate() : new Date().getDate()
      const hh = new Date().getHours() < 10 ? '0' + new Date().getHours() : new Date().getHours()
      const mf = new Date().getMinutes() < 10 ? '0' + new Date().getMinutes() : new Date().getMinutes()
      const ss = new Date().getSeconds() < 10 ? '0' + new Date().getSeconds() : new Date().getSeconds()
      _this.gettime = yy + '-' + mm + '-' + dd + ' ' + hh + ':' + mf + ':' + ss
      console.log(_this.gettime)
    },
  }

在这里插入图片描述

第二种

获取当前时间和一小时前时间,但是格式需要调整,将时间格式化

  created() {
    
    
    this.getCurrentTime()
  },
  methods: {
    
    
    getCurrentTime() {
    
    
      var frontOneHour = new Date(new Date().getTime() - 1 * 60 * 60 * 1000);
      console.log(new Date(new Date().getTime() - 1 * 60 * 60 * 1000), new Date()) // 前一个小时  当前时间
      console.log(frontOneHour)
    },
  }

在这里插入图片描述

第三种

采用封装好的工具,将时间格式化

@/utils/index.js 中代码

/**
 * Parse the time to string
 * @param {(Object|string|number)} time
 * @param {string} cFormat
 * @returns {string | null}
 */
export function parseTime(time, cFormat) {
    
    
  if (arguments.length === 0 || !time) {
    
    
    return null
  }
  const format = cFormat || '{
    
    y}-{
    
    m}-{
    
    d} {
    
    h}:{
    
    i}:{
    
    s}'
  let date
  if (typeof time === 'object') {
    
    
    date = time
  } else {
    
    
    if ((typeof time === 'string')) {
    
    
      if ((/^[0-9]+$/.test(time))) {
    
    
        // support "1548221490638"
        time = parseInt(time)
      } else {
    
    
        // support safari
        // https://stackoverflow.com/questions/4310953/invalid-date-in-safari
        time = time.replace(new RegExp(/-/gm), '/')
      }
    }

    if ((typeof time === 'number') && (time.toString().length === 10)) {
    
    
      time = time * 1000
    }
    date = new Date(time)
  }
  const formatObj = {
    
    
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{
    
    ([ymdhisa])+}/g, (result, key) => {
    
    
    const value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') {
    
     return ['日', '一', '二', '三', '四', '五', '六'][value ] }
    return value.toString().padStart(2, '0')
  })
  return time_str
}

在项目页面中引入

import {
    
     parseTime } from '@/utils/index'

JS

  created() {
    
    
    this.getCurrentTime()
  },
  methods: {
    
    
    getCurrentTime() {
    
    
      var frontOneHour = new Date(new Date().getTime() - 1 * 60 * 60 * 1000)
      console.log(new Date(new Date().getTime() - 1 * 60 * 60 * 1000), new Date()) // 前一个小时  当前时间
      console.log(frontOneHour)
      const CurrentTime = parseTime(new Date(), '{
    
    y}-{
    
    m}-{
    
    d} {
    
    h}:{
    
    i}:{
    
    s}')
      const fileName = parseTime(frontOneHour, '{
    
    y}-{
    
    m}-{
    
    d} {
    
    h}:{
    
    i}:{
    
    s}')
      console.log(CurrentTime)
      console.log(fileName)
      // var frontOneHour = new Date(new Date().getTime() - 1 * 60 * 60 * 1000) 一小时前
      // var frontOneDay = new Date(new Date().getTime() - 24 * 60 * 60 * 1000) 一天前
      // var frontSevenDay = new Date(new Date().getTime() - 7 * 24 * 60 * 60 * 1000) 七天前
      // const currentTime = parseTime(new Date(), '{y}-{m}-{d} {h}:{i}:{s}') 格式化当前时间
      // const frontOneHourTime = parseTime(frontOneHour, '{y}-{m}-{d} {h}:{i}:{s}') 格式化一小时前时间
      // const frontOneDayTime = parseTime(frontOneDay, '{y}-{m}-{d} {h}:{i}:{s}') 格式化一天前时间
      // const frontSevenDayTime = parseTime(frontSevenDay, '{y}-{m}-{d} {h}:{i}:{s}') 格式化七天前时间
    },
  }

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/Aohanzzz/article/details/126883701