js常用技巧汇总

简介: js常用技巧汇总

获取当前日期的周一、周末的日期

export function getWeeekdata(cdate){  //cdate 传来当前的时间
   let now = new Date(cdate);
   let year = now.getFullYear();
   let month = now.getMonth() + 1;
   let date = now.getDate();
   let nowTime = now.getTime();
   let day = now.getDay();
   let oneDayTime = 24 * 60 * 60 * 1000;
   if(day==0){
    var MondayTime = nowTime - 6 * oneDayTime;
    var SundayTime = nowTime;
   }else if(day==1){
     var MondayTime = nowTime;
     var SundayTime = nowTime + (7 - day) * oneDayTime;
   }else{
     var MondayTime = nowTime - (day - 1) * oneDayTime;
     //显示周日
     var SundayTime = nowTime + (7 - day) * oneDayTime;
   }
   //初始化日期时间
   var monday = new Date(MondayTime);
   var sunday = new Date(SundayTime);
   return [format(monday), format(sunday)];
 }
 function format(date) {
   var time = new Date(date);
   var y = time.getFullYear();
   var m =
     time.getMonth() + 1 < 10 ? "0" + (time.getMonth() + 1) : time.getMonth() + 1;
   var d = time.getDate() < 10 ? "0" + time.getDate() : time.getDate();
   //var h = time.getHours();
   //var mm = time.getMinutes();
   //var s = time.getSeconds();
   return y + "-" + m + "-" + d;
 }
复制代码

根据当前时间获取当月的1号和最后一号

 export function getcurentMonth(cdate) { //cdate传来的当前的时间
   // 当天
   let thatDay = "";
   // 当月第一天
   let oneDayTime = "";
   // 当月最后一天
   let zDay = "";
   let date = new Date(cdate);
   let curr_date = date.getDate();
   let curr_month = date.getMonth() + 1;
   let curr_year = date.getFullYear();
   String(curr_month).length < 2 ? (curr_month = "0" + curr_month) : curr_month;
   String(curr_date).length < 2 ? (curr_date = "0" + curr_date) : curr_date;
   thatDay = curr_year + "-" + curr_month + "-" + curr_date;
   String(curr_year).length < 2 ? (curr_year = "0" + curr_year) : curr_year;
   var m = date.getMonth() + 1;
   String(m).length < 2 ? (m = "0" + m) : m;
   var d = "01";
   oneDayTime = curr_year + "-" + m + "-" + d;
   //结束时间
   var currentMonth = date.getMonth();
   var nextMonth = ++currentMonth;
   var nextMonthFirstDay = new Date(date.getFullYear(), nextMonth, 1);
   var oneDay = 1000 * 60 * 60 * 24;
   var date1 = new Date(nextMonthFirstDay - oneDay);
   var yy = date1.getFullYear();
   String(yy).length < 2 ? (yy = "0" + yy) : yy;
   var mm = date1.getMonth() + 1;
   String(mm).length < 2 ? (mm = "0" + mm) : mm;
   var dd = date1.getDate();
   String(dd).length < 2 ? (dd = "0" + dd) : dd;
   zDay = yy + "-" + mm + "-" + dd;
   return [thatDay, oneDayTime, zDay];
 }
复制代码

判断文件上传的类型

/**
 * @param: fileName - 文件名称
 * @param: 数据返回 1) 无后缀匹配 - false
 * @param: 数据返回 2) 匹配图片 - image
 * @param: 数据返回 3) 匹配 txt - txt
 * @param: 数据返回 4) 匹配 excel - excel
 * @param: 数据返回 5) 匹配 word - word
 * @param: 数据返回 6) 匹配 pdf - pdf
 * @param: 数据返回 7) 匹配 ppt - ppt
 * @param: 数据返回 8) 匹配 视频 - video
 * @param: 数据返回 9) 匹配 音频 - radio
 * @param: 数据返回 10) 其他匹配项 - other
 * @author: ljw
 **/
export function fileSuffixTypeUtil(fileName){
      // 后缀获取
    var suffix = "";
    // 获取类型结果
    var result = "";
    try {
      var flieArr = fileName.split(".");
      suffix = flieArr[flieArr.length - 1];
    } catch (err) {
      suffix = "";
    }
    // fileName无后缀返回 false
    if (!suffix) {
      result = false;
      return result;
    }
    // 图片格式
    var imglist = ["png", "jpg", "jpeg", "bmp", "gif"];
    // 进行图片匹配
    result = imglist.some(function (item) {
      return item == suffix;
    });
    if (result) {
      result = "image";
      return result;
    }
    // 匹配txt
    var txtlist = ["txt"];
    result = txtlist.some(function (item) {
      return item == suffix;
    });
    if (result) {
      result = "txt";
      return result;
    }
    // 匹配 excel
    var excelist = ["xls", "xlsx"];
    result = excelist.some(function (item) {
      return item == suffix;
    });
    if (result) {
      result = "excel";
      return result;
    }
    // 匹配 word
    var wordlist = ["doc", "docx"];
    result = wordlist.some(function (item) {
      return item == suffix;
    });
    if (result) {
      result = "word";
      return result;
    }
    // 匹配 pdf
    var pdflist = ["pdf"];
    result = pdflist.some(function (item) {
      return item == suffix;
    });
    if (result) {
      result = "pdf";
      return result;
    }
    // 匹配 ppt
    var pptlist = ["ppt"];
    result = pptlist.some(function (item) {
      return item == suffix;
    });
    if (result) {
      result = "ppt";
      return result;
    }
    // 匹配 视频
    var videolist = ["mp4", "m2v", "mkv"];
    result = videolist.some(function (item) {
      return item == suffix;
    });
    if (result) {
      result = "video";
      return result;
    }
    // 匹配 音频
    var radiolist = ["mp3", "wav", "wmv"];
    result = radiolist.some(function (item) {
      return item == suffix;
    });
    if (result) {
      result = "radio";
      return result;
    }
    // 其他 文件类型
    result = "other";
    return result;
};
复制代码

文件下载

/**
 * @param: fileType - 文件类型
 * @param: fileName - 文件名称
 * @param: data  - 数据流文件
 **/
export function download(fileType, bucketName,data) {
  let downType = "";
  let downName = "";
  if (fileType == "image") {
    downType = "image/png";
    downName = fileName + ".png";
  } else if (fileType == "word") {
    downType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
    downName = fileName + ".docx";
  } else if (fileType == "video") {
    downType = "video/mpeg4";
    downName = fileName + ".mp4";
  } else if (fileType == "radio") {
    downType = "audio/mpeg";
    downName = fileName + ".mp3";
  } else if (fileType == "pdf") {
    downType = "application/pdf";
    downName = fileName + ".pdf";
  }
  let blob = new Blob([data], { type: downType });
  let downloadElement = document.createElement("a");
  let href = window.URL.createObjectURL(blob);
  downloadElement.href = href;
  document.body.appendChild(downloadElement);
  //downloadElement.setAttribute("download", downName);//设置下载名称
  downloadElement.download = downName; //设置下载文件名称
  downloadElement.click();
  document.body.removeChild(downloadElement); //移除元素;防止连续点击创建多个a标签
  window.URL.revokeObjectURL(href);
}

大厂面试题分享 面试题库

前后端面试题库 (面试必备) 推荐:★★★★★

地址:前端面试题库  web前端面试题库 VS java后端面试题库大全

相关文章
|
3月前
|
缓存 前端开发 算法
Next.js
Next.js【8月更文挑战第4天】
58 1
|
3月前
|
JavaScript 前端开发
JS中比较
JS中比较
29 0
|
3月前
|
JavaScript
什么是js?
什么是js?
147 0
|
6月前
|
JavaScript 前端开发
初识JS
这篇内容介绍了JavaScript的基础知识,包括DOM和BOM(相当于内置模块和第三方模块)、变量的声明(var、let、const的特点和区别)、数组操作(如push、pop、splice等)、字典操作以及DOM操作示例。变量部分强调了var的变量提升和块级作用域问题,let和const的块级作用域和不可重复声明,以及const用于创建不可变的常量。在实际应用中,展示了如何动态改变HTML元素内容和创建新元素的示例。
|
人工智能 JavaScript 前端开发
js的转变
js的转变
50 0
|
JavaScript
js超实用的小技巧(1)
js超实用的小技巧(1)
|
JavaScript
js有什么用
js有什么用
89 0
|
JavaScript 前端开发 算法
JS之BigNumber.js 讲解
JS之BigNumber.js 讲解
1250 0
JS之BigNumber.js 讲解
|
JavaScript 前端开发
3、JS总结
3、JS总结
67 0
3、JS总结
|
JavaScript
js中的this
js中的this