/*
 * @Author: ymy
 * @Date: 2020-08-13 17:42:52
 * @LastEditors: ymy
 * @LastEditTime: 2020-10-10 16:09:09
 * @Description: file content
 */
export default function Format(date:any,fmt:any){
  if(!date) return ''
  if(!(date instanceof Date)){
    date=new Date(date)
  }
  const o:any = {   
    "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(!fmt){
    // fmt="yyyy-MM-dd HH:mm:ss"
    fmt="yyyy-MM-dd"
  }
  if(fmt == 'week'){
    //本年的第一天
    const beginDate = new Date(date.getFullYear(), 0, 1);
    //星期从0-6,0代表星期天，6代表星期六
    let endWeek = date.getDay();
    if (endWeek == 0) endWeek = 7;
    let beginWeek = beginDate.getDay();
    if (beginWeek == 0) beginWeek = 7;
    //计算两个日期的天数差
    const millisDiff = date.getTime() - beginDate.getTime();
    const dayDiff = Math.floor(( millisDiff + (beginWeek - endWeek) * (24 * 60 * 60 * 1000)) / 86400000);
    // return date.getFullYear() +' 年第 ' + Math.ceil(dayDiff / 7) + 1 + ' 周';
    return Math.ceil(dayDiff / 7) + 1

  }else{
    if(/(y+)/.test(fmt))
      fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length))
    for(const k in o)   
      if(new RegExp("("+ k +")").test(fmt))   
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
      return fmt;  
  }
}
  