时间格式的处理集合 JavaScript
1.从一个周一开始计算,某年有多少个周,周的开始日期和结束日期
function yearWeek(year) { var d = new Date(year, 0, 1); while (d.getDay() != 1) { d.setDate(d.getDate() 1); } var to = new Date(year 1, 0, 1); var i = 1; for (var from = d; from < to;) { document.write(year "年第" i "周 " (from.getMonth() 1) "月" from.getDate() "日 - "); from.setDate(from.getDate() 6); if (from < to) { document.write((from.getMonth() 1) "月" from.getDate() "日<br / >"); from.setDate(from.getDate() 1); } else { to.setDate(to.getDate() - 1); document.write((to.getMonth() 1) "月" to.getDate() "日<br / >"); } i ; }}yearWeek(2020);
2.获取今天是本月的第几周(从本年的第一个周一开始计算)
function getWeekStr(str) { // 将字符串转为标准时间格式 str2 = Date.parse(str); let date = new Date(str2); let month = date.getMonth() 1; let week = getWeekFromDate(date); if (week === 0) {//第0周归于上月的最后一周 month = date.getMonth(); let dateLast = new Date(); let dayLast = new Date(dateLast.getFullYear(), dateLast.getMonth(), 0).getDate(); let timestamp = new Date(new Date().getFullYear(), new Date().getMonth() - 1, dayLast); week = getWeekFromDate(new Date(timestamp)); } let time = month "月第" week "周"; return time;}function getWeekFromDate(date) { // 将字符串转为标准时间格式 let w = date.getDay();//周几 if (w === 0) { w = 7; } let week = Math.ceil((date.getDate() 6 - w) / 7) - 1; return week;}getWeekStr("2020-12-08");
3.两个时间相差多少分钟
function minutesCha(starTime, end) { //定义两个变量time1,time2分别保存开始和结束时间 var time1 = starTime; var time2 = end; //判断开始时间是否大于结束日期 if (time1 > time2) { alert("开始时间不能大于结束时间!"); return false; } //用split把字符串分隔成数组 var starTime1 = time1.substr(0, 10).split("-"); var end1 = time2.substr(0, 10).split("-"); //将拆分的数组重新组合,并实例成化新的日期对象 var date1 = new Date(starTime1[1] - starTime1[2] - starTime1[0]); var date2 = new Date(end1[1] - end1[2] - end1[0]); //得到两个日期之间的差值m,以分钟为单位 //Math.abs(date2-date1)计算出以毫秒为单位的差值 //Math.abs(date2-date1)/1000得到以秒为单位的差值 //Math.abs(date2-date1)/1000/60得到以分钟为单位的差值 var m = parseInt(Math.abs(date2 - date1) / 1000 / 60); //小时数和分钟数相加得到总的分钟数 //time1.substr(11,2)截取字符串得到时间的小时数 //parseInt(time1.substr(11,2))*60把小时数转化成为分钟 var min1 = parseInt(time1.substr(11, 2)) * 60 parseInt(time1.substr(14, 2)); var min2 = parseInt(time2.substr(11, 2)) * 60 parseInt(time2.substr(14, 2)); //两个分钟数相减得到时间部分的差值,以分钟为单位 var n = min2 - min1; //将日期和时间两个部分计算出来的差值相加,即得到两个时间相减后的分钟数 var minutes = m n; // 返回 分钟差 return minutes; }minutesCha('2020-12-08 15:17:52','2020-12-08 16:17:52');
4.获取当前时间
function changeStyle() { var times = new Date(); var years = times.getFullYear(); var mouths = times.getMonth() 1; var datas = times.getDate(); var hours = times.getHours() var minute = times.getMinutes() var seconds = times.getSeconds() if (mouths <= 9) { mouths = "0" mouths; } if (datas <= 9) { datas = "0" datas; } if (hours <= 9) { hours = "0" hours; } if (minute <= 9) { minute = "0" minute; } if (seconds <= 9) { seconds = "0" seconds; } var sendTime1 = years "-" mouths "-" datas ' ' hours ':' minute ':' seconds var sendTime2 = years "" mouths "" datas '' hours '' minute '' seconds var sendTime3 = years "/" mouths "/" datas ' ' hours ':' minute ':' seconds var sendTime = [sendTime1, sendTime2,sendTime3] return sendTime;}changeStyle();
5.获取本周,上周,下周……
function getweeks(weeknum) { var date = new Date(); var currentFirstDate = new Date(date); var formatDate = function(date) { var year = date.getFullYear(); //年 var month = (date.getMonth() 1); //月 var day = date.getDate(); //日; if (month < 10) { month = '0' month; } return month '.' day }; var addDate = function(date, n) { date.setDate(date.getDate() n); return date; }; var setDate = function(date) { var week = date.getDay() - 1; date = addDate(date, week * -1); currentFirstDate = new Date(date); var weekList = []; for (var i = 0; i < 7; i ) { weekList[i] = formatDate(i == 0 ? date : addDate(date, 1)); } return weekList }; // var today = formatDate(date); //今日日期 var a = new Array("日", "一", "二", "三", "四", "五", "六"); var week = new Date().getDay(); // var thisWeek=setDate(addDate(currentFirstDate,0));//本周 // var lastWeek=setDate(addDate(currentFirstDate,-7));//上周 var weeks = setDate(addDate(currentFirstDate, weeknum)) if(a[week]=="日"){ return weeks=setDate(addDate(currentFirstDate,-7)) }else{ return weeks }}getweeks(0);//本周getweeks(-7);//上周getweeks(7);//下周
6.根据年月日,获取是本年第几周
function getWeek(y, m, d) { let day1 = new Date(y, parseInt(m) - 1, d); let day2 = new Date(y, 0, 1); let day = Math.round((day1.valueOf() - day2.valueOf()) / 86400000); return Math.ceil((day ((day2.getDay() 1) - 1)) / 7) - 1}
7.将秒转化为时分秒
function formatSeconds(value) { let result = parseInt(value) let h = Math.floor(result / 3600); let m = Math.floor((result / 60 % 60)); let s = Math.floor((result % 60)); let res = ''; if (h != 0) res = `${h}时`; if (m != 0) res = `${m}分`; if (s != 0) res = `${s}秒`; return res;}
8.获取指定时间段内的年月
function getMonthBetween(){ let start="2019-01-01"; let end="2020-01-01"; var result = []; var s = start.split("-"); var e = end.split("-"); var min = new Date(); var max = new Date(); min.setFullYear(s[0],s[1]); max.setFullYear(e[0],e[1]); // console.log(e[0] "---- " e[1]); var curr = min; while(curr <= max){ var month = curr.getMonth(); // month=month==0?12:month; var str=curr.getFullYear() "-" (month); var s=curr.getFullYear() "-0"; if(str==s){ str=curr.getFullYear()-1 "-12"; result.unshift({ year:curr.getFullYear()-1, month:'12月' }); }else{ result.unshift({ year:curr.getFullYear(), month:month '月' }); } curr.setMonth(month 1); } return result; }getMonthBetween();
9.将日期转化成时间戳
function getUnixTime(dateStr){ var newstr = dateStr.replace(/-/g,'/'); var date = new Date(newstr); var time_str = date.getTime().toString(); return time_str.substr(0, 10);}getUnixTime('2020-12-08 16:43:03');
10.过滤掉时间间隔符(正则)
var time='2020-12-08 16:43:03'console.log(time.replace(/\s/g,"").replace(/[^0-9]/g,"").toLowerCase())
本文是借鉴,整理集合
赞 (0)