美文网首页
获取某个时间的函数(对象传参,支持自定义格式和自定义分隔符)

获取某个时间的函数(对象传参,支持自定义格式和自定义分隔符)

作者: jack_rofer | 来源:发表于2020-12-04 17:36 被阅读0次

在线演示

/*
*1.函数名:getSomeDateToFormat
*2.传参:一个对象,里面有三个参数,均为可选
*3-1.queryDate:【指定时间参数】
*   ①参考https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#Examples传参
*   ②注意:最好传字符串,如:2012-12-12 12:12:12
*3-2.needFormat:【指定格式参数】
*   ①传date,生成 yyyy-MM-dd
*   ②传dateTime,生成 yyyy-MM-dd hh:mm:ss
*   ③传time,生成 hh:mm:ss
*   ④传自定义对象:{year:'1',month:'1',day:'1',hours:'1',minutes:'1',seconds:'1'},给什么键,返回对应字符,值随便传,不为false就行。
*3-3.seperator:【指定分隔符参数】
    ①传自定义对象:{year:'年',month:'月',day:'日',hours:'时',minutes:'分',seconds:'秒'},根据键返回值。
*/
function getSomeDateToFormat({queryDate, needFormat, seperator} = {}){
  if(!queryDate){
    queryDate = new Date()
  }
  let date = new Date(queryDate);
  let Y = date.getFullYear();
  let M = date.getMonth()+1;
  let D = date.getDate();
  let h = date.getHours();
  let m = date.getMinutes();
  let s = date.getSeconds();
  M = M<10? '0'+ M: M;
  D = D<10? '0'+ D: D;
  h = h<10? '0'+ h: h;
  m = m<10? '0'+ m: m;
  s = s<10? '0'+ s: s;
  let result = '',
      
      filterY = needFormat && needFormat.year? Y: '',
      filterM = needFormat && needFormat.month? M: '',
      filterD = needFormat && needFormat.day? D: '',
      filterh = needFormat && needFormat.hours? h: '',
      filterm = needFormat && needFormat.minutes? m: '',
      filters = needFormat && needFormat.seconds? s: '',
      
      //有分隔符属性
      theSepYear = seperator && seperator.year? seperator.year: '-',
      theSepMonth = seperator && seperator.month? seperator.month: '-',
      theSepDay = seperator && seperator.day? seperator.day: '',
      theSepHours = seperator && seperator.hours? seperator.hours: ':',
      theSepMinutes = seperator && seperator.minutes? seperator.minutes: ':',
      theSepSeconds = seperator && seperator.seconds? seperator.seconds: '',      
      
      //有时间才给分隔符
      sepYear = filterY && seperator && seperator.year? seperator.year: '',
      sepMonth = filterM && seperator && seperator.month? seperator.month: '',
      sepDay = filterD && seperator && seperator.day? seperator.day: '',
      sepHours = filterh && seperator && seperator.hours? seperator.hours: '',
      sepMinutes = filterm && seperator && seperator.minutes? seperator.minutes: '',
      sepSeconds = filters && seperator && seperator.seconds? seperator.seconds: '';
      
      
  if(!needFormat && !seperator){
    return `${Y}-${M}-${D} ${h}:${m}:${s}`
  }else{
    if(needFormat && !seperator){
      if(Object.prototype.toString.call(needFormat) === '[object Object]'){
        return result = `${filterY}${sepYear}${filterM}${sepMonth}${filterD}${sepDay} ${filterh}${sepHours}${filterm}${sepMinutes}${filters}${sepSeconds}`
      }
      switch(needFormat){
          case 'date':
          result = `${Y}-${M}-${D}`
          break;
          case 'dateTime':
          result = `${Y}-${M}-${D} ${h}:${m}:${s}`
          break;
          case 'time':
          result = `${h}-${m}-${s}`
          break;
          default: result = `${Y}-${M}-${D} ${h}:${m}:${s}`
      }
    }else if(!needFormat && seperator){
      if(Object.prototype.toString.call(seperator) !== '[object Object]'){
        return result = `${Y}-${M}-${D} ${h}:${m}:${s}`
      }
      result = `${Y}${theSepYear}${M}${theSepMonth}${D}${theSepDay} ${h}${theSepHours}${m}${theSepMinutes}${s}${theSepSeconds}`
           
    }else if (needFormat && seperator){
      if(Object.prototype.toString.call(needFormat) === '[object Object]'){
        return result = `${filterY}${sepYear}${filterM}${sepMonth}${filterD}${sepDay} ${filterh}${sepHours}${filterm}${sepMinutes}${filters}${sepSeconds}`
      }
      switch(needFormat){
          case 'date':
          result = `${Y}${theSepYear}${M}${theSepMonth}${D}${theSepDay}`
          break;
          case 'dateTime':
          result = `${Y}${theSepYear}${M}${theSepMonth}${D}${theSepDay} ${h}${theSepHours}${m}${theSepMinutes}${s}${theSepSeconds}`
          break;
          case 'time':
          result = `${h}${theSepHours}${m}${theSepMinutes}${s}${theSepSeconds}`
          break;
          default: result = `${Y}${theSepYear}${M}${theSepMonth}${D}${theSepDay} ${h}${theSepHours}${m}${theSepMinutes}${s}${theSepSeconds}`
      }      
    }
  }
  return result
}

调用:

const params = {
         queryDate: '',
         needFormat: {year: '1',month: '1',day: '1',hours: '1',minutes: '1', seconds: '1'}, 
         seperator: {year: '年',month: '月',day: '日',hours: '时',minutes: '分', seconds: '秒'}
}
const time = getSomeDateToFormat(params )
console.log('time', time)

相关文章

网友评论

      本文标题:获取某个时间的函数(对象传参,支持自定义格式和自定义分隔符)

      本文链接:https://www.haomeiwen.com/subject/ozyiwktx.html