es6常用基础工具

1. 判断是否为数字型字符串:/^[0-9]+.?[0-9]*/.test(value)

2. 删除数组中的特定元素:arr.splice(arr.findIndex(item => item.id === data.id), 1)

3. 中国标准时间转换成字符串(带时分秒)
export function getTimeFormat(chinaTime) {
  const d = new Date(chinaTime)
  const year = d.getFullYear()
  const month = d.getMonth() + 1 < 10 ? '0' + (d.getMonth() + 1) : d.getMonth() + 1
  const date = d.getDate() < 10 ? '0' + d.getDate() : d.getDate()
  const hh = d.getHours() < 10 ? '0' + d.getHours() : d.getHours()
  const mm = d.getMinutes() < 10 ? '0' + d.getMinutes() : d.getMinutes()
  const ss = d.getSeconds() < 10 ? '0' + d.getSeconds() : d.getSeconds()
  const week = d.getDay()
  return year + '-' + month + '-' + date + ' ' + hh + ':' + mm + ':' + ss
}

4. 中国标准时间转换成字符串
export function getTimeFormatShort(chinaTime) {
  const d = new Date(chinaTime)
  const year = d.getFullYear()
  const month = d.getMonth() + 1 < 10 ? '0' + (d.getMonth() + 1) : d.getMonth() + 1
  const date = d.getDate() < 10 ? '0' + d.getDate() : d.getDate()
  return year + '-' + month + '-' + date
}
 
5. 将驼峰命名转换成下划线命名
export function toLine(name) {
  return name.replace(/([A-Z])/g, '_$1').toLowerCase()
}
posted @ 2021-04-19 20:11  一个爱好城建的程序猿  阅读(107)  评论(0)    收藏  举报