Compare two times in js

Posted by stephenf33 on Sun, 19 Dec 2021 03:22:50 +0100

Demand: set a collection time for unpaid items. The user can customize the collection time. If the current time exceeds the collection time set by the user, an alert prompt will pop up when the operation and management personnel log in to the system

This article focuses on!!!
js for time difference, I have sorted out three implementation methods. The first two are compared with the current time, and the last is compared with the current time. Generally speaking, it is almost the same. For me, the application environment is only different

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)

The third one is separated here. The following codes are combined in the project in the same way. It depends on how you write them:

new Date().getTime()-new Date(yjxx[0][i].YJSJ.replace(/-/g, '/')).getTime())/(60*60*1000)

When I first realized this, I wanted to write a timing function in the database to execute, because the version used in the database is PG9 4. After consulting relevant materials, I learned that pgAgent needs to be installed. It doesn't seem so easy to operate at once. Then I thought about it and changed to this implementation idea

In the main page, import js files (newly created files):

<script type="text/javascript" src="../common/default/showyjts.js;"></script>

Since you need to judge the time and give a prompt as soon as you log in to the system, you need to use window Onload, the method is called here

 window.onload = function () {
    debugger;
    if(appCode=='work'){
        debugger;
        showyjxx(); 
    }
};

The sql method is encapsulated in the system, which can directly operate the database in js. These operations can be ignored directly. In the method, judge whether the current login person is an early warning position and whether it meets the conditions of judging time

function showyjxx(){
    debugger
    //Judge whether it is the person in charge of the early warning project
    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){ //Early warning post
    let yjxx = runSQL(`select xmbh,xmmc,SSDW,YJSJ,recid from fyxmgl.xmxxb where sfct = 'yes' and sfqt = 'yes' and sffk = 'no' 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(/ [am | PM] / g, '')// Get current time
    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(`Project number is[ ${yjxx[0][0].XMBH}]The payment of your project has exceeded the time limit, please contact us as soon as possible ${yjxx[0][0].SSDW}The person in charge of the unit shall handle relevant payment!!!`);
                    let upyjsj = runSQL(`update fyxmgl.xmxxb set yjbs = 1 where recid = ${yjxx[0][i].RECID};`);
                }
            }
        }
    }
    var bodyheight = $("body")[0].clientHeight;//Get height
    var bodywidth = $("body")[0].clientWidth;//Get width  
    }
}

The following similar contents in this article can be ignored, but the query statement method encapsulated in the system I use,

var shijian = runSQL("select sbsj,jzsj from rxaj where recid = ${recId};");
yjxx[0][i].YJSJ

Topics: Javascript ECMAScript regex