/********** 日期处理函数 *********/
<script type="text/javascript" src="${springMacroRequestContext.contextPath}/assets/js/calendar/WdatePicker.js"></script>
<input class="btn btn-sm btn-default" type="button" onclick="upday(this);" id="upday" value="上一天"></input>
<input class="btn btn-sm btn-default" type="button" onclick="today(this);" id="today" value="今天"></input>
<input class="btn btn-sm btn-default" type="button" onclick="nextday(this);" id="nextday" value="下一天"></input>
<script type="text/javascript">
$(document).ready(function() {
$('#tableBill').DataTable({
"ajax": {
"url": getContext() + "********",
"type": "POST",
"data": function(param) {
param.billing_type = $("#billing_type").val();
param.Stime = $("#startTime").val();
param.Etime = $("#endTime").val();
param.show_type = $("#show_type").val();
}
},
/* 查询结果 */
function getResult() {
$('#tableBill').DataTable().ajax.reload(null, false);
if ($("#show_type").val() === "month") {
document.getElementById("upday").value = "上一月";
document.getElementById("today").value = "本月";
document.getElementById("nextday").value = "下一月";
} else {
document.getElementById("upday").value = "上一天";
document.getElementById("today").value = "今天";
document.getElementById("nextday").value = "下一天";
}
}
/*时间转化函数*/
Date.prototype.format = function(format) {
var o = {
"M+": this.getMonth() + 1, //month
"d+": this.getDate(), //day
"h+": this.getHours(), //hour
"m+": this.getMinutes(), //minute
"s+": this.getSeconds(), //second
"q+": Math.floor((this.getMonth() + 3) / 3), //quarter
"S": this.getMilliseconds() //millisecond
}
if (/(y+)/.test(format)) {
format = format.replace(RegExp.$1, (this.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 GetDateStr(AddDayCount) {
var dd = null;
if ($("#startTime").val() == null || $("#startTime").val() == "" && $("#endTime").val() == null || $("#endTime").val() == "") {
dd = new Date();
} else {
dd = new Date(Date.parse($("#endTime").val()));
}
dd.setDate(dd.getDate() + AddDayCount); //获取AddDayCount天后的天期
var y = dd.getFullYear();
var m = dd.getMonth() + 1; //获取当前月份的天期
if (m < 10) {
m = "0" + m;
}
var d = dd.getDate();
if (d < 10) {
d = "0" + d;
}
return y + "-" + m + "-" + d;
}
/*根据传入天期获取当月最后一天的时间*/
function getLastDay(date,num) {
if (date==null || date=="") {
date = new Date().format("yyyy-MM-dd");
}
var new_year = date.split("-")[0]; //取当前的年份
var new_month = date.split("-")[1]++;//取下一个月的第一天,方便计算(最后一天不固定)
if(date.split("-")[1]>12) {
new_month -=12; //月份减
new_year++; //年份增
}
new_month=+new_month+num;
if(new_month>12) {
new_month -=12; //月份减
new_year++; //年份增
}
if (new_month<10) {
new_month = "0"+new_month;
}
if (new_month<1) {
new_month = 12 ;
new_year--;
}
var new_date = new Date(new_year,new_month,01);
return new_year+"-"+new_month+"-"+(new Date(new_date.getTime()-1000*60*60*24)).getDate();//获取当月最后一天日期
}
/*根据传入天期获取当月第一天的时间*/
function getFirstDay(date,num){
if (date==null || date=="") {
date = new Date().format("yyyy-M-dd");
}
var new_year = date.split("-")[0]; //取当前的年份
var new_month = date.split("-")[1]; //取出来当前月份
new_month=+new_month+num;
if(new_month>12) {
new_month -=12; //月份减
new_year++; //年份增
}
if (new_month<10) {
new_month = "0"+new_month;
}
if (new_month<1) {
new_month =12;
new_year--;
}
return new_year+"-"+new_month+"-"+"01";
}
/*查询上一天、月*/
function upday(obj) {
if (obj.value == "上一天") {
document.getElementById("startTime").value = GetDateStr(-2);
document.getElementById("endTime").value = GetDateStr(-1);
$('#tableBill').DataTable().ajax.reload(null, false);
} else if (obj.value == "上一月") {
document.getElementById("startTime").value = getFirstDay($("#endTime").val(),-1);
document.getElementById("endTime").value = getLastDay($("#endTime").val(),-1);
$('#tableBill').DataTable().ajax.reload(null, false);
}
}
/*查询本天、月*/
function today(obj) {
if (obj.value == "今天") {
document.getElementById("startTime").value = "";
document.getElementById("endTime").value = "";
$('#tableBill').DataTable().ajax.reload(null, false);
} else if (obj.value == "本月") {
document.getElementById("startTime").value = "";
document.getElementById("endTime").value = "";
$('#tableBill').DataTable().ajax.reload(null, false);
}
}
/*查询下一天、月*/
function nextday(obj) {
if (obj.value == "下一天") {
var now = new Date();
var nowStr = now.format("yyyy-MM-dd");
if ($("#startTime").val() == null || $("#startTime").val() == "" &&
$("#endTime").val() == null || $("#endTime").val() == "" || $("#endTime").val() == nowStr) {
javascript: void(0);
} else {
document.getElementById("startTime").value = GetDateStr(0);
document.getElementById("endTime").value = GetDateStr(+1);
$('#tableBill').DataTable().ajax.reload(null, false);
}
} else if (obj.value == "下一月") {
var now = new Date();
var nowStr = now.format("yyyy-MM-dd");
if ($("#startTime").val() == null || $("#startTime").val() == "" &&
$("#endTime").val() == null || $("#endTime").val() == "" || $("#startTime").val() == getFirstDay(nowStr,0)) {
javascript: void(0);
} else {
document.getElementById("startTime").value = getFirstDay($("#endTime").val(),+1);
document.getElementById("endTime").value = getLastDay($("#endTime").val(),+1);
$('#tableBill').DataTable().ajax.reload(null, false);
}
}
}
</script>