js获取选中日期的当周的周一和周日
刚找这个的js方法,找到了下面的博客:
https://www.cnblogs.com/nelsonlei/p/10071659.html
不知道为什么要把东西写的这么复杂。。。。
我自己写了个:
function getDate(type,date){ let d=date || new Date() let s=d.format("yyyy-MM-dd") if(type=="当天"){ return [s,s] }else if(type=="本周"){ let t=d.getTime() let day=d.getDay() day=day==0?7:day let d1= t-(day-1)*24*60*60*1000 let d2= t+(7-day)*24*60*60*1000 return [new Date(d1).format("yyyy-MM-dd"),new Date(d2).format("yyyy-MM-dd")] }else if(type=="本月"){ let arr=s.split("-") return [arr[0]+"-"+arr[1]+"-01",arr[0]+"-"+arr[1]+"-"+run_nian(d)] } } let max_month=[1,3,5,7,8,10,12] function run_nian(d){ let month=d.getMonth()+1 if(month==2){ let year=d.getFullYear() if(year % 100 ==0){ if(year % 400 ==0){ return 29 } return 28 }else{ return 28 } }else{ if($.inArray(month,max_month)>=0){ return 31 }else{ return 30 } } }
/** * 日期格式化 * @param {Object} fmt 格式化字符串,例:‘yyyy-MM-dd HH:mm:ss’ */ Date.prototype.format = function(fmt) { //author: meizz var o = { "M+": this.getMonth() + 1, //月份 "d+": this.getDate(), //日 "H+": this.getHours(), //小时 "m+": this.getMinutes(), //分 "s+": this.getSeconds(), //秒 "q+": Math.floor((this.getMonth() + 3) / 3), //季度 "S": this.getMilliseconds() //毫秒 }; if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)); for (var 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; }
当然我这个复杂是有三种,单个的比那个简单多了。
分类:
JavaScript
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构