//获取日期 0为今天,负数为前几天,正数为后几天 function getDay(day){ var today = new Date(); var targetday_milliseconds = today.getTime() + 1000*60*60*24*day; today.setTime(targetday_milliseconds); var tYear = today.getFullYear(); var tMonth = today.getMonth(); var tDay = today.getDate(); tMonth = doHandleZero(tMonth + 1); tDay = doHandleZero(tDay); return tYear + "-" + tMonth + "-" + tDay; } //获取当前时分秒时间 function getNowTime(){ var date = new Date(); var hour = doHandleZero(date.getHours()); var minute = doHandleZero(date.getMinutes()); var second = doHandleZero(date.getSeconds()); return getDay(0) + ' ' + hour + ':' + minute + ':' + second; } //获取本周第一天 function getFirstDayOfWeek() { var date = new Date(); var weekday = date.getDay()||7; //获取星期几,getDay()返回值是 0(周日) 到 6(周六) 之间的一个整数。0||7为7,即weekday的值为1-7 date.setDate(date.getDate()-weekday+1);//往前算(weekday-1)天,年份、月份会自动变化 var tYear = date.getFullYear(); var tMonth = date.getMonth(); var tDay = date.getDate(); tMonth = doHandleZero(tMonth + 1); tDay = doHandleZero(tDay); return tYear + "-" + tMonth + "-" + tDay; } //获取本月第一天 function getFirstDayOfMonth(){ var today = new Date(); var tYear = today.getFullYear(); var tMonth = today.getMonth(); tMonth = doHandleZero(tMonth + 1); return tYear + "-" + tMonth + "-01"; } //获取上个月第一天 function getFirstDayOfLastMonth(){ var today = new Date(); var tYear = today.getFullYear(); var tMonth = today.getMonth(); tMonth = doHandleZero(tMonth); return tYear + "-" + tMonth + "-01"; } //获取上个月最后一天 function getLastDayOfLastMonth(){ var today = new Date(); let lastMonthDay = new Date(today.getFullYear(), today.getMonth(), 0).getDate(); //获取月份最后一天 return today.getFullYear() + "-" + doHandleZero(today.getMonth()) + "-" + lastMonthDay; } //获取24小时之前的小时时间 function get24HourAfter(){ var today = new Date(); var tYear = today.getFullYear(); var tMonth = today.getMonth(); var tDay = today.getDate(); var hour = doHandleZero(today.getHours()); tMonth = doHandleZero(tMonth + 1); tDay = doHandleZero(tDay-1); return tYear + "-" + tMonth + "-" + tDay + ' ' + hour + ":00:00"; } //获取一小时之前的小时时间 function getOneHourAgo(){ var today = new Date(); var tYear = today.getFullYear(); var tMonth = today.getMonth(); var tDay = today.getDate(); var hour = doHandleZero(today.getHours()-1); tMonth = doHandleZero(tMonth + 1); tDay = doHandleZero(tDay); return tYear + "-" + tMonth + "-" + tDay + ' ' + hour + ":00:00"; } //获取今天第一天 function getFirstDayOfYear(){ var today = new Date(); var tYear = today.getFullYear(); return tYear + "-01-01"; } //获取去年第一天 function getFirstDayOfLastYear(){ var today = new Date(); var tYear = today.getFullYear(); return (tYear - 1) + "-01-01"; } //获取两个日期之间相差的天数 function getDaysForTwo(start, end){ var date1 = start + ' 00:00:00'; //开始时间 var date2 = end?end:new Date(); //结束时间 var date3 = date2.getTime() - new Date(date1).getTime(); //时间差的毫秒数 //var subMinutes = Math.floor( date3/(60*1000) ) //获取总共的分钟差 //计算出相差天数 var days = Math.floor(date3/(24*3600*1000)) return days } //获取两个日期之间相差的小时数 function getHoursForTwo(start, end){ var date1 = start; //开始时间 var date2 = end?end:new Date(); //结束时间 var date3 = date2.getTime() - new Date(date1).getTime(); //时间差的毫秒数 var hours = Math.floor( date3/(3600*1000) ) //获取总共的分钟差 //计算出相差天数 //var days = Math.floor(date3/(24*3600*1000)) return hours } //获取两个日期之间相差的分钟数 function getMinutesForTwo(start, end){ var date1 = start; //开始时间 var date2 = end?end:new Date(); //结束时间 var date3 = date2.getTime() - new Date(date1).getTime(); //时间差的毫秒数 var minutes = Math.floor( date3/(60*1000) ) //获取总共的分钟差 return minutes } //日期格式转换 格林威治时间转yyyy-MM-dd HH:mm:ss function transferDate(date, format) { format = format || 'yyyy-MM-dd HH:mm:ss'; date = new Date(date); date.setHours(date.getHours()-14); var o = { 'M+' : date.getMonth() + 1, //month 'd+' : date.getDate(), //day 'H+' : date.getHours(), //hour 'm+' : date.getMinutes(), //minute 's+' : date.getSeconds(), //second 'q+' : Math.floor((date.getMonth() + 3) / 3), //quarter 'S' : date.getMilliseconds() //millisecond }; if (/(y+)/.test(format)) format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length)); for (var k in o) if (new RegExp('(' + k + ')').test(format)) format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length)); return format; } function doHandleZero(month){ var m = month; if(month.toString().length == 1){ m = "0" + month; } return m; } function dateToString(date){ var year = date.getFullYear(); var month =(date.getMonth() + 1).toString(); var day = (date.getDate()).toString(); if (month.length == 1) { month = "0" + month; } if (day.length == 1) { day = "0" + day; } var dateTime = year + "-" + month + "-" + day; return dateTime; }