Jquery时间戳转换为字符串,今天,昨天,本月第一天,本周第一天。
1 //获取当前日期 2 function getTime(dateStr,pattern){ 3 let dt = new Date(dateStr) 4 let date = shellDate(dt) 5 if(pattern && pattern.toLowerCase() === 'yyyy-mm-dd'){ 6 return date 7 }else{ 8 let hh = dt.getHours().toString().padStart(2,'0') 9 let mm = dt.getMinutes().toString().padStart(2,'0') 10 let ss = dt.getSeconds().toString().padStart(2,'0') 11 return `${date} ${hh}:${mm}:${ss}` 12 } 13 } 14 15 //获取本月第一天 16 function timeFormat() { 17 let date = new Date() 18 date.setDate(1) 19 return shellDate(date) 20 } 21 //获取昨天的日期 22 function yesterDay(){ 23 let date = new Date(); 24 date.setTime(date.getTime()-24*60*60*1000); 25 return shellDate(date) 26 } 27 28 //本周第一天 29 function weeksFirst(){ 30 let date=new Date(); 31 let nowTime = date.getTime() ; 32 let day = date.getDay() || 7;//周一是每周的第一天 33 //let day = date.getDay() //周日是每周的第一天 34 let oneDayTime = 24*60*60*1000 ; 35 36 let Monday = nowTime - (day-1)*oneDayTime ; 37 let mondayTime=new Date(Monday); 38 return shellDate(mondayTime) 39 } 40 //时间格式转换 41 function shellDate(date) { 42 let y = date.getFullYear() 43 let m = (date.getMonth()+1).toString().padStart(2, '0') 44 let d = date.getDate().toString().padStart(2,'0') 45 return `${y}-${m}-${d}` 46 }