JS 日期格式转换 2020-12-08

//日期时间转换  formData(New Date() ,'yyyy-MM-dd')

export function formData(dates,format) {

  if(dates == ''||dates ==null){

    return '';

  }else{

    let date = new Date(dates)

    let time = {

     'M+': date.getMonth() + 1,

     'd+': date.getDate(),

     'h+': date.getHours(),

     'm+': date.getMinutes(),

     's+': date.getSeconds(),

     'q+': Math.floor((date.getMonth() + 3) / 3),

     'S+': date.getMilliseconds(),

   };

   if (/(y+)/i.test(format)) {

     format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))

   }

   for(let k in time){

     if (new RegExp('(' + k + ')').test(format)) {

       format = format. replace(RegExp.$1, RegExp.$1.length === 1 ? time[k] : ('00' + time[k]).substr(('' + time[k]) . length));

     }

   }

   return format;

  }


//  }

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容