前端处理时间格式

需求:将后端返回的时间格式处理为:年-月-日 时: 分: 秒   格式

解决:

一. 创建filters文件,在该文件下创建index.js,filters文件位置如图:

 二. index.js代码如下

​

import moment from 'moment'
export { parseTime, formatTime } from '@/utils'

/**
 * 
 * @param {number} time
 * @param {string} label
 * @return {string}
 */
function pluralize (time, label) {
  if (time === 1) {
    return time + label
  }
  return time + label + 's'
}

/**
 * @param {number} time
 */
export function timeAgo (time) {
  const between = Date.now() / 1000 - Number(time)
  if (between < 3600) {
    return pluralize(~~(between / 60), ' minute')
  } else if (between < 86400) {
    return pluralize(~~(between / 3600), ' hour')
  } else {
    return pluralize(~~(between / 86400), ' day')
  }
}

/**
 * 
 * 
 * @param {number} num
 * @param {number} digits
 */
export function numberFormatter (num, digits) {
  const si = [
    { value: 1E18, symbol: 'E' },
    { value: 1E15, symbol: 'P' },
    { value: 1E12, symbol: 'T' },
    { value: 1E9, symbol: 'G' },
    { value: 1E6, symbol: 'M' },
    { value: 1E3, symbol: 'k' }
  ]
  for (let i = 0; i < si.length; i++) {
    if (num >= si[i].value) {
      return (num / si[i].value).toFixed(digits).replace(/\.0+$|(\.[0-9]*[1-9])0+$/, '$1') + si[i].symbol
    }
  }
  return num.toString()
}

/**
 * 10000 => "10,000"
 * @param {number} num
 */
export function toThousandFilter (num) {
  return (+num || 0).toString().replace(/^-?\d+/g, m => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','))
}

/**
 * 大写首字符
 * @param {String} string
 */
export function uppercaseFirst (string) {
  return string.charAt(0).toUpperCase() + string.slice(1)
}

/**
 * 判断默认显示0
 * @param {String} string
 */
export function toZero (string) {
  return !string ? 0 : string
}

/**
 * 日期格式化为年月日 : 2000年08月16日
 * @param {String} string
 */
export function formatDate (value, formatString) {
  formatString = formatString || 'YYYY-MM-DD'
  return value ? moment(value).format(formatString) : ''
}

/**
 * 日期格式化为年月日时分秒 : 2000年08月16日 17:00
 * @param {String} string
 */
export function formatDateTime (value, formatString) {
  formatString = formatString || 'YYYY-MM-DD HH:mm:ss'
  return value ? moment(value).format(formatString) : ''
}

​

三. 引入utils的文件链接 提取码为4444

https://pan.baidu.com/s/1IezO_vdZxQlewlUHbod4cQ

四. 在main.js中引入filtersmoment,如图

五. 在所需要处理的标签上加入formatDateTime,如图

最终效果:

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值