来看图、看需求,获取今日,昨天,近7天,近30天的时间,格式根据自己需求可自定义。2021-04-16 00:00~~2021-04-16 23:59
上代码
日期代码,自己可在控制台打印查看
// 如果只是简单的格式化只变成2021-04-16 19:39:24,需要运用三目运算判断并在适当的地方加上0,完成所需要的格式。
var date1 = new Date(new Date(new Date().toLocaleDateString()).getTime());
var date2 = new Date(new Date(new Date().toLocaleDateString()).getTime()+24*60*60*1000-1);
var todayStart = date1.getFullYear() + "-" + ((date1.getMonth() + 1) < 10 ? "0" + (date1.getMonth() + 1):(date1.getMonth() + 1))+ "-" + (date1.getDate() < 10 ? "0" + date1.getDate():date1.getDate()) + " " + (date1.getHours()<10?"0"+date1.getHours():date1.getHours()) + ":" + (date1.getMinutes()<10?"0"+date1.getMinutes():date1.getMinutes()) + ":" + (date1.getSeconds()<10?"0"+date1.getSeconds():date1.getSeconds())
var startTime = date1.getFullYear() + "-" + ((date1.getMonth() + 1) < 10 ? "0" + (date1.getMonth() + 1):(date1.getMonth() + 1))+ "-" + (date1.getDate() < 10 ? "0" + date1.getDate():date1.getDate()-1) + " " + (date1.getHours()<10?"0"+date1.getHours():date1.getHours()) + ":" + (date1.getMinutes()<10?"0"+date1.getMinutes():date1.getMinutes()) + ":" + (date1.getSeconds()<10?"0"+date1.getSeconds():date1.getSeconds())
var endTime = date2.getFullYear() + '-' + (date2.getMonth() + 1) + '-' + (date2.getDate()-1) + ' ' + date2.getHours() + ':' + date2.getMinutes()+ ":" + (date1.getSeconds()<10?"0"+date1.getSeconds():date1.getSeconds())
var weekDateStart = date1.getFullYear() + "-" + ((date1.getMonth() + 1) < 10 ? "0" + (date1.getMonth() + 1):(date1.getMonth() + 1))+ "-" + (date1.getDate() < 10 ? "0" + date1.getDate():date1.getDate()-7) + " " + (date1.getHours()<10?"0"+date1.getHours():date1.getHours()) + ":" + (date1.getMinutes()<10?"0"+date1.getMinutes():date1.getMinutes()) + ":" + (date1.getSeconds()<10?"0"+date1.getSeconds():date1.getSeconds())
var weekDateEnd = date2.getFullYear() + '-' + (date2.getMonth() + 1) + '-' + (date2.getDate()-7) + ' ' + date2.getHours() + ':' + date2.getMinutes()+ ":" + (date1.getSeconds()<10?"0"+date1.getSeconds():date1.getSeconds())
var monthDateStart = date1.getFullYear() + "-" + ((date1.getMonth() + 1) < 10 ? "0" + (date1.getMonth() ):(date1.getMonth() + 1)-1)+ "-" + (date1.getDate() < 10 ? "0" + date1.getDate():date1.getDate()) + " " + (date1.getHours()<10?"0"+date1.getHours():date1.getHours()) + ":" + (date1.getMinutes()<10?"0"+date1.getMinutes():date1.getMinutes()) + ":" + (date1.getSeconds()<10?"0"+date1.getSeconds():date1.getSeconds())
var monthDateEnd = date2.getFullYear() + '-' + (date2.getMonth() +1) + '-' + (date2.getDate()) + ' ' + date2.getHours() + ':' + date2.getMinutes()+ ":" + (date1.getSeconds()<10?"0"+date1.getSeconds():date1.getSeconds())
// todayStart 今天开始日期 startTime 昨日开始日期 endTime 昨日结束日期
// weekDateStart 近7天开始日期 weekDateEnd 近7天结束日期
// monthDateStart 近30天开始日期 monthDateEnd 近30天结束日期
全部代码
// 如果只是简单的格式化只变成2021-04-16 19:39:24,需要运用三目运算判断并在适当的地方加上0,完成所需要的格式。
var date1 = new Date(new Date(new Date().toLocaleDateString()).getTime());
var date2 = new Date(new Date(new Date().toLocaleDateString()).getTime()+24*60*60*1000-1);
var todayStart = date1.getFullYear() + "-" + ((date1.getMonth() + 1) < 10 ? "0" + (date1.getMonth() + 1):(date1.getMonth() + 1))+ "-" + (date1.getDate() < 10 ? "0" + date1.getDate():date1.getDate()) + " " + (date1.getHours()<10?"0"+date1.getHours():date1.getHours()) + ":" + (date1.getMinutes()<10?"0"+date1.getMinutes():date1.getMinutes())
var startTime = date1.getFullYear() + "-" + ((date1.getMonth() + 1) < 10 ? "0" + (date1.getMonth() + 1):(date1.getMonth() + 1))+ "-" + (date1.getDate() < 10 ? "0" + date1.getDate():date1.getDate()-1) + " " + (date1.getHours()<10?"0"+date1.getHours():date1.getHours()) + ":" + (date1.getMinutes()<10?"0"+date1.getMinutes():date1.getMinutes())
var endTime = date2.getFullYear() + '-' + (date2.getMonth() + 1) + '-' + (date2.getDate()-1) + ' ' + date2.getHours() + ':' + date2.getMinutes()
var weekDateStart = date1.getFullYear() + "-" + ((date1.getMonth() + 1) < 10 ? "0" + (date1.getMonth() + 1):(date1.getMonth() + 1))+ "-" + (date1.getDate() < 10 ? "0" + date1.getDate():date1.getDate()-7) + " " + (date1.getHours()<10?"0"+date1.getHours():date1.getHours()) + ":" + (date1.getMinutes()<10?"0"+date1.getMinutes():date1.getMinutes())
var weekDateEnd = date2.getFullYear() + '-' + (date2.getMonth() + 1) + '-' + (date2.getDate()-7) + ' ' + date2.getHours() + ':' + date2.getMinutes()
// todayStart 今天开始日期 startTime 昨日开始日期 endTime 昨日结束日期
// weekDateStart 近7天开始日期 weekDateEnd 近7天结束日期
function nowDate(){ //当前时间
let today =new Date();
let h = today.getFullYear();
let m = today.getMonth()+1;
let d = today.getDate();
let d1 = today.getTime();
console.log(d1)
let hour=today.getHours();
let mint=today.getMinutes();
let miao=today.getSeconds();
m = m < 10 ? '0' + m :m;
d = d < 10 ? '0' + d :d;
console.log( '当前时间:',h + '-' + m + '-' + d + ' ' + hour+':'+ mint+':'+ miao)
return h + '-' + m + '-' + d + ' ' + hour + ':'+ mint;
}
function publicDate(num){ //获取当前时间的公共方法 7天、30天、90天、一年传入对应的天数就可以了
let today =new Date(new Date() - num*24*60*60*1000);
let h = today.getFullYear();
let m = today.getMonth()+1;
let d = today.getDate();
let hour=date1.getHours()<10?"0"+date1.getHours():date1.getHours();//date1 是00:00:00 date2是23:59:59
let mint=date1.getMinutes()<10?"0"+date1.getMinutes():date1.getMinutes();
let miao=today.getSeconds();
m = m < 10 ? '0' + m :m;
d = d < 10 ? '0' + d :d;
miao = miao < 10 ? '0' + miao :miao;
console.log( '当前时间:',h + '-' + m + '-' + d + hour+':'+ mint+':'+ miao)
return h + '-' + m + '-' + d+ ' ' + hour + ':'+ mint;
}
function selectDate(obj){ //客服呼叫排名 日期选择
let val = $(obj).val();
let num
if(val == "近7天"){ //近7天
num = 7
$('.comDate').val(weekDateStart)
$('.nowDate2').val(weekDateEnd)
console.log('val:',val)
console.log(weekDateStart,"----",weekDateEnd)
}else if(val == "今日"){ //今日
$('.comDate').val(todayStart)
$('.nowDate2').val(nowDate())
console.log('val:',val)
console.log(todayStart,"----",nowDate())
}else if(val == "昨日"){ //昨日
$('.comDate').val(startTime)
$('.nowDate2').val(endTime)
console.log('昨日:',val)
console.log(startTime,"----",endTime)
}else if(val == "近30天"){ //近30天
num = 30
publicDate(num)
$('.comDate').val(publicDate(num))
$('.nowDate2').val(nowDate())
console.log('近30天:',val)
}else if(val == "近90天"){ //近90天
num = 90
publicDate(num)
$('.comDate').val(publicDate(num))
$('.nowDate2').val(nowDate())
console.log('近90天:',val)
}else{ //自定义
console.log('val:',val)
}
}
html代码
<div class="com_dataTime_select" style="width: 650px;right:2px;">
<div class="checkin-message">
<div style="width:100px;">日期选择</div>
<div class=" g-select distribution-room-select">
<input type="text" value="今日" class="input-height select_img ipt">
<select onchange="selectDate(this)">
<option value="今日">今日</option>
<option value="昨日">昨日</option>
<option value="近7天">近7天</option>
<option value="自定义">自定义</option>
</select>
</div>
<div class="" >丨 </div>
<div>
<input type="text" class="g-input Wdate f-left comDate" value="2021-01-01 17:57" onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm'})" readonly>
<span style="display: inline-block;position: relative;left: 64px;">~</span>
<input type="text" class="g-input Wdate f-left nowDate2" value="2021-12-31 17:57" onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm'})" readonly>
</div>
</div>
</div>