将时间戳转化为2018-10-10 18:00:00格式
function toTime(timestamp){
let date = new Date(timestamp);
//PHP时间戳为10位需*1000,其他时间戳为13位的不需乘1000
let Y = date.getFullYear() + '-';
let M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
let D = date.getDate() + ' ';
let h = date.getHours() + ':';
let m = date.getMinutes() + ':';
let s = date.getSeconds();
return Y+M+D+h+m+s;
}
获取当前时间,以横线拼接
function timestampToTime2(){
let date = new Date();
let Y = date.getFullYear();
let M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1);
let D = date.getDate() < 10 ? '0'+date.getDate() : date.getDate();
let h = date.getHours() < 10 ? '0'+date.getHours() : date.getHours();
let m = date.getMinutes() < 10 ? '0'+date.getMinutes() : date.getMinutes();
let s = date.getSeconds() < 10 ? '0'+date.getSeconds() : date.getSeconds();
return Y+'-'+M+'-'+D+' '+h+':'+m+':'+s;
}
其他获取本周本月本季度的时间段
// 获取当天时间段
function time(){
let time = new Date();
let now = time.getTime();
let todayStart = time.getFullYear()+'-'+(time.getMonth()+1)+'-'+time.getDate()+'T'+'0:0:0'+'Z';
let todayEnd = time.getFullYear()+'-'+(time.getMonth()+1)+'-'+time.getDate()+'T'+'23:59:59'+'Z';
// if(!new Date(todayStart).getTime()){
// todayStart = todayStart.replace(/-/g,'/');
// todayEnd = todayEnd.replace(/-/g,'/');
// }
return([todayStart,todayEnd])
// console.log(new Date(todayStart),new Date(todayEnd))
// return [new Date(todayStart),new Date(todayEnd)];
};
// console.log(time())
// 获取本月时间段
function month(){
let month = new Date();
let monthStart = month.getFullYear()+'-'+(month.getMonth()+1)+'-'+1+' '+'0:0:0';
// console.log(month.getMonth())
if (month.getMonth() <= 10){
var monthEnd = month.getFullYear()+'-'+(month.getMonth()+2)+'-'+1+' '+'0:0:0';
} else {
var monthEnd = (month.getFullYear()+1)+'-'+1+'-'+1+' '+'0:0:0';
}
// if(!new Date(monthStart).getTime()){
// monthStart = monthStart.replace(/-/g,'/');
// monthEnd = monthEnd.replace(/-/g,'/');
// }
// return [new Date(monthStart),new Date(monthEnd)];
return [monthStart,monthEnd];
};
// 获取本周时间段
function week(){
let now = new Date(); //当前日期
let nowDayOfWeek = now.getDay(); //今天本周的第几天
let nowDay = now.getDate(); //当前日
let nowMonth = now.getMonth(); //当前月
let nowYear = now.getYear()+1900; //当前年
let weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);
let weekEndDate = new Date(nowYear, nowMonth, nowDay + (7 - nowDayOfWeek));
weekStartDate = toTime(Date.parse(weekStartDate));
weekEndDate = toTime(Date.parse(weekEndDate));
// if(!new Date(weekStartDate).getTime()){
// weekStartDate = weekStartDate.replace(/-/g,'/');
// weekEndDate = weekEndDate.replace(/-/g,'/');
// }
return [weekStartDate,weekEndDate];
};
// 获取本季度时间段
function quarter(){
let now = new Date();
let nowYear = now.getYear()+1900;//年
let nowMonth = now.getMonth(); //当前月
let start = '';
let end = '';
if(nowMonth < 3){
start = nowYear+'-1-1 0:0:0';
end = nowYear+'-4-1 0:0:0';
}else if (nowMonth >= 3 && nowMonth < 6){
start = nowYear+'-4-1 0:0:0';
end = nowYear+'-7-1 0:0:0';
}else if(nowMonth >= 6 && nowMonth< 9){
start = nowYear+'-7-1 0:0:0';
end = nowYear+'-10-1 0:0:0';
}else if (nowMonth >= 9){
start = nowYear+'-10-1 0:0:0';
end = (nowYear+1)+'-1-1 0:0:0';
}
// if(!new Date(nowYear+'-1-1 0:0:0').getTime()){
// start = start.replace(/-/g,'/');
// end = end.replace(/-/g,'/');
// return [new Date(start),new Date(end)];
// }
// return [new Date(start),new Date(end)];
return [start,end];
};
注意ie中时间是2018/10/10 18:00:00格式
replace(/-/g,'/')
用"/"替换"-"即可。