需求:针对未缴费的项目设置一个回款时间,用户自定义回款时间,如果当前时间超过用户设置的回款时间,则需要给经营管理人员登录系统时,弹出预警提示
本文重点!!!
js求时间差,我整理了三种实现方式,前两种是两个时间做对比,最后一种是和当前时间做对比,总体来说其实差不多,对我而言,单单是应用环境不一样
NO1
function getHour(s1,s2) {
s1 = new Date(s1.replace(/-/g, '/'));
s2 = new Date(s2.replace(/-/g, '/'));
var ms = Math.abs(s1.getTime() - s2.getTime());
return ms / 1000 / 60 / 60;
}
alert( getHour('2021-6-20 10:45:23', '2021-8-29 11:07:25'))
NO2
var shijian = runSQL("select sbsj,jzsj from rxaj where recid = ${recId};");
if(shijian[0].length>0){
var strDate1 = shijian[0][0].SBSJ;
//var strDate2 = shijian[0][0].JZSJ;
var strDate2 = $("#1399").textbox("getValue");
strDate1=new Date(strDate1.replace(/-/g, '/'));
strDate2=new Date(strDate2.replace(/-/g, '/'));
var sj = Math.abs(strDate1.getTime() - strDate2.getTime());
var sjc = sj/(60*60*1000);
NO3
let myDate = new Date();
let s1 = myDate.getTime();
let s2 = new Date('2021-6-20 10:45:23'.replace(/-/g, '/')).getTime();
let sjc = ( s1 - s2 )/(60*60*1000)
第三种我这里分开了,下边我代码在项目中是合起来的,方法都是一样的,看习惯怎么写:
new Date().getTime()-new Date(yjxx[0][i].YJSJ.replace(/-/g, '/')).getTime())/(60*60*1000)
一开始实现这里的时候,我想的是在数据库中写一个定时函数去执行,由于数据库使用的版本是pg9.4,查阅相关资料得知这个需要安装pgAgent,一顿操作下来看起来也并不是呢么容易,然后左思右想换成了这种实现思路
在主页面中,引入js文件(自己新创建文件):
<script type="text/javascript" src="../common/default/showyjts.js;"></script>
由于需要用户一登陆系统,就判断时间,给出提示,因此需要使用window.onload,方法在这里边调用
window.onload = function () {
debugger;
if(appCode=='work'){
debugger;
showyjxx();
}
};
系统中封装了sql方法,可以直接在js中操作数据库,这些操作可以直接忽略,在方法中判断当前登录人员是否为预警岗位,并判断是否符合判断时间的条件
function showyjxx(){
debugger
//判断是否为预警项目负责人
let humansql = runSQL("select roleid from fyxmgl.tbrolehuman where humanid = "+humanId+";");
let humandata='';
if(humansql[0].length>0){
for(var i=0;i<humansql[0].length;i++){
humandata += humansql[0][i].ROLEID+",";
}
humandata = humandata.substr(0,humandata.length-1);
}
debugger;
if(humandata.indexOf("368047")!=-1){ //预警岗位
let yjxx = runSQL(`select xmbh,xmmc,SSDW,YJSJ,recid from fyxmgl.xmxxb where sfct = '是' and sfqt = '是' and sffk = '否' and recid in
(select distinct recid from fyxmgl.tbactinst where actdefid = 6 and completed = 0 and partid = ${humanId});`);
let yjxxdata = [];
// let myDate = new Date().toLocaleString().replace(/[上午|下午]/g,'');//获取当前时间
let myDate = new Date();
if(yjxx[0].length>0){
for(var i=0;i<yjxx[0].length;i++){
if(yjxx[0][i].YJSJ != null){
if((myDate.getTime()-new Date(yjxx[0][i].YJSJ.replace(/-/g, '/')).getTime())/(60*60*1000)>0){
esys.alert(`项目编号为【${yjxx[0][0].XMBH}】的项目已缴费超时,请尽快联系${yjxx[0][0].SSDW}单位负责人,办理相关缴费!!!`);
let upyjsj = runSQL(`update fyxmgl.xmxxb set yjbs = 1 where recid = ${yjxx[0][i].RECID};`);
}
}
}
}
var bodyheight = $("body")[0].clientHeight;//获取高度
var bodywidth = $("body")[0].clientWidth;//获取宽度
}
}
对于文章中存在的以下类似内容,可以忽略,只是我所用的系统中封装的查询语句方法,
var shijian = runSQL("select sbsj,jzsj from rxaj where recid = ${recId};");
yjxx[0][i].YJSJ
最终实现效果:
选择设置回款时间
若超出选定时间,下次登录系统会弹出提示框