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);
}


相关文章
|
12月前
|
存储 缓存 Java
自动装配机制
本文深入解析SpringBoot自动装配机制,从@SpringBootApplication注解入手,剖析其组合注解原理。重点讲解@AutoConfigurationPackage通过@Import实现包扫描路径注册,以及@AutoConfigurationImportSelector如何借助SpringFactoriesLoader加载spring.factories中配置的自动装配类,结合条件注解实现智能化配置,最终实现“约定优于配置”的核心思想。(238字)
|
9天前
|
域名解析 人工智能 Linux
|
19天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
17540 63
|
17天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
3天前
|
人工智能 安全 网络安全
|
7天前
|
云安全 数据采集 人工智能
|
8天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1382 5
|
14天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2439 8
|
19天前
|
弹性计算 人工智能 API
Codex超详细入门指南(下载·安装·登录·上手,全版本覆盖,2026最新版)
建议收藏本教程,2026 全年各版本更新随时回来查阅。如果你在搭建过程中遇到任何问题,欢迎结合 阿里云工单系统 提交技术支持请求,或在 阿里云开发者社区 搜索 Codex 相关话题交流。 祝你编码愉快,效率翻倍!
3945 22
|
20天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是DeepSeek AI开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、跑命令、处理表格、联网搜索等操作。免费开源,兼容Windows/macOS/Linux,提供桌面端与网页端,可灵活安装插件扩展能力。

热门文章

最新文章