helper.ts 2.16 KB
/*
 * @Author: yangming yangming@didihu.com.cn
 * @Date: 2023-11-28 14:20:48
 * @LastEditors: yangming yangming@didihu.com.cn
 * @LastEditTime: 2023-12-11 09:55:03
 * @FilePath: \glsx-tracker-javascript\src\utils\helper.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
export default {
  dateFormat(timestamp: number | string | Date, format = 'YYYY-MM-DD HH:mm:ss'): string {
    var date = new Date(timestamp)
    function fixedTwo(value: number): string {
      return value < 10 ? '0' + value : String(value)
    }
    var showTime = format
    if (showTime.includes('SSS')) {
      const S = date.getMilliseconds()
      showTime = showTime.replace('SSS', '0'.repeat(3 - String(S).length) + S)
    }
    if (showTime.includes('YY')) {
      const Y = date.getFullYear()
      showTime = showTime.includes('YYYY') ? showTime.replace('YYYY', String(Y)) : showTime.replace('YY', String(Y).slice(2, 4))
    }
    if (showTime.includes('M')) {
      const M = date.getMonth() + 1
      showTime = showTime.includes('MM') ? showTime.replace('MM', fixedTwo(M)) : showTime.replace('M', String(M))
    }
    if (showTime.includes('D')) {
      const D = date.getDate()
      showTime = showTime.includes('DD') ? showTime.replace('DD', fixedTwo(D)) : showTime.replace('D', String(D))
    }
    if (showTime.includes('H')) {
      const H = date.getHours()
      showTime = showTime.includes('HH') ? showTime.replace('HH', fixedTwo(H)) : showTime.replace('H', String(H))
    }
    if (showTime.includes('m')) {
      var m = date.getMinutes()
      showTime = showTime.includes('mm') ? showTime.replace('mm', fixedTwo(m)) : showTime.replace('m', String(m))
    }
    if (showTime.includes('s')) {
      var s = date.getSeconds()
      showTime = showTime.includes('ss') ? showTime.replace('ss', fixedTwo(s)) : showTime.replace('s', String(s))
    }
    return showTime
  },
  trim(str: string) {
    return str.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '')
  },
  extend(to: any, _from: any): Object {
    for (const key in _from) {
      to[key] = _from[key]
    }
    return to
  }
}