在做web的时候,我们常常有这样的需求,设置搜索条件,查找某一段时间段的列表数据。
开始时间(beginTime)到截止时间(endTime)、近三天、近七天、本周、本月等等,这些都涉及到对时间的处理。
这里即总结一下JS对时间的一些处理和格式化操作:
var formatDate = function(date, type) {
var y = date.getFullYear();
var m = date.getMonth() + 1;
m = m < 10 ? '0' + m : m;
var d = date.getDate();
d = d < 10 ? ('0' + d) : d;
if (type == true) {
return y + '-' + m + '-' + d;
} else {
return y + '' + m + '' + d;
}
};
var now = new Date();//当前日期
console.log(now)//Mon Jul 30 2018 10:56:05 GMT+0800 (中国标准时间)
var nowDayOfWeek = now.getDay();//今天本周的第几天
console.log(nowDayOfWeek)//1
var nowDay = now.getDate();//当前日
console.log(nowDay)//30
var nowMonth = now.getMonth();//当前月
console.log(nowMonth)//6
var nowYear = now.getYear();//当前年
nowYear += (nowYear < 2000) ? 1900 : 0; //
console.log(nowYear)//2018
var nowTime = formatDate(new Date());//今日日期
console.log(nowTime)//20180730
var beginTimeWeek = formatDate(new Date(nowYear, nowMonth, nowDay - nowDayOfWeek));//本周第一天的日期
console.log(beginTimeWeek)//20180729
var endTimeWeek = formatDate(new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek)));//本周的结束日期
console.log(endTimeWeek)//20180804
var beginTimeMonth = formatDate(new Date(nowYear, nowMonth, 1));//本月第一天的日期
console.log(beginTimeMonth)//20180701
var endTimeMonth = formatDate(new Date(nowYear, nowMonth, getMonthDays(nowMonth)))//本月的结束日期
console.log(endTimeMonth)//20180731
//获得今年某月的天数
function getMonthDays(myMonth){
var monthStartDate = new Date(nowYear, myMonth, 1);
var monthEndDate = new Date(nowYear, myMonth + 1, 1);
return (monthEndDate - monthStartDate)/(1000 * 60 * 60 * 24);
}
var daysNo = getMonthDays(1);//今年2月份的天数
console.log(daysNo)//28
//获得本季度的开始月份
function getQuarterStartMonth(){
var quarterStartMonth = 0;
if(nowMonth<3){
quarterStartMonth = 0;
}
if(2<nowMonth && nowMonth<6){
quarterStartMonth = 3;
}
if(5<nowMonth && nowMonth<9){
quarterStartMonth = 6;
}
if(nowMonth>8){
quarterStartMonth = 9;
}
return quarterStartMonth;
}
console.log(getQuarterStartMonth());//6
//得到标准时间的时间
function getLocalTime(i) {
//参数i为时区值数字,比如北京为东八区则输入8,西五则输入-5
var i = i?parseFloat(i):8;
//得到本地时间
var d = new Date();
//得到1970年一月一日到现在的秒数
var local = d.getTime();
//本地时间与GMT时间的时间偏移差
var offset = d.getTimezoneOffset()*60*1000;
//得到现在的格林尼治时间
var utcTime = local + offset;
return new Date(utcTime + 60*60*1000*i);
}
console.log(getLocalTime())//Mon Jul 30 2018 14:54:54 GMT+0800 (中国标准时间)
console.log(getLocalTime(7))//Mon Jul 30 2018 13:54:54 GMT+0800 (中国标准时间)
//转换服务器时区时间
function formatTime(t,utc){
//t传入的时间参数,utc传入的时区参数
if(!t) return;
//获取本地时间
var d=new Date();
//获得本地时区
utc=utc?parseFloat(utc):d.getTimezoneOffset()/60;
//格式化传入时间
var time=new Date(t);
//转换传入时间为本地时间(默认传入服务器时间为东八区时间)
time.setHours(time.getHours()+(utc-8));
//输出时间
var yy=time.getFullYear();
var MM=time.getMonth()+1;
MM=MM<10?'0'+MM:MM;
var dd=time.getDate();
dd=dd<10?'0'+dd:dd;
var hh=time.getHours();
hh=hh<10?'0'+hh:hh;
var mm=time.getMinutes();
mm=mm<10?'0'+mm:mm;
var ss=time.getSeconds();
ss=ss<10?'0'+ss:ss;
var date=yy+'-'+MM+'-'+dd+' '+hh+':'+mm+':'+ss;
return date;
}
console.log(formatTime(new Date(), 8));//2018-07-30 14:54:54
console.log(formatTime(new Date(), 7));//2018-07-30 13:54:54