how to calculate salary of last week in javascript?

872 Views Asked by At

I'm trying to calculate salary of last week of employees. It means current week should not me included in the calculation. Right now I have -7 from current date to check my data. This is my code

var currentDate = new Date();
log.info(currentDate)
var requiredDate=new Date(currentDate.getFullYear(),currentDate.getMonth(),currentDate.getDate()-7)
var start=Date.parse(requiredDate)
var end=Date.parse(currentDate);
query="taskCreateTimestamp:["+start+" TO "+end+"]";

My objective is to calculate last week salary between Monday to friday. I have taken -7 to check my data only. Please help me

3

There are 3 best solutions below

0
On

check this link you can get start date and end date of last week. and append time stamp '00:00:00' to start date and '23:59:59' to the end date if your data have time stamp.

get last week in javascript

1
On

"(salary / (working days of month)) * (working days of last week)" Above line should be your formula for calculating last week salary. So, you need a method which calculates working days of given date interval.

function getWorkingDays(startDate, endDate){
    var totalWorkingDays= 0;
    var currentDate = startDate;
    while (currentDate <= endDate)  {
        var weekDay = currentDate.getDay();
        if(weekDay != 0 && weekDay != 6) {
            totalWorkingDays++;
        }
        currentDate.setDate(currentDate.getDate()+1); 
    }
    return totalWorkingDays;
}

Then, give the dates and apply the formula.

0
On

I have used below code in Jaggeryjs and working fine for me. I haven't tested yet but getting expected result.

 var currentDate = new Date();
    var requiredDate;
    log.info(currentDate)
    var set=currentDate.setHours(0)
    if(currentDate.getDay() == 1)
    {
     requiredDate=new Date(currentDate.getFullYear(),currentDate.getMonth(),currentDate.getDate()-7)
    }
    else if(currentDate.getDay() == 2)
    {
     requiredDate=new Date(currentDate.getFullYear(),currentDate.getMonth(),currentDate.getDate()-8)
    }
    else if(currentDate.getDay() == 3)
    {
     requiredDate=new Date(currentDate.getFullYear(),currentDate.getMonth(),currentDate.getDate()-9)
    }
    else if(currentDate.getDay() == 4)
    {
     requiredDate=new Date(currentDate.getFullYear(),currentDate.getMonth(),currentDate.getDate()-10)
    }
    else if(currentDate.getDay() == 5)
    {
     requiredDate=new Date(currentDate.getFullYear(),currentDate.getMonth(),currentDate.getDate()-4)
    }
    else if(currentDate.getDay() == 6)
    {
     requiredDate=new Date(currentDate.getFullYear(),currentDate.getMonth(),currentDate.getDate()-5)
    }
    else if(currentDate.getDay() == 7)
    {
     requiredDate=new Date(currentDate.getFullYear(),currentDate.getMonth(),currentDate.getDate()-6)
    }
    var start=Date.parse(requiredDate)
    log.info("Start-Date["+start+"]")         
    log.info("End----------------------------------------------")
    var currentDate = new Date();
    log.info(currentDate)       
    var end;
    if(currentDate.getDay() == 7)
    {
     end=new Date(currentDate.getFullYear(),currentDate.getMonth(),currentDate.getDate()-2)
    }
    else if(currentDate.getDay() == 6)
    {
     end=new Date(currentDate.getFullYear(),currentDate.getMonth(),currentDate.getDate()-1)
    }

    else if(currentDate.getDay() == 5)
    {
     end=Date.parse(currentDate)
     log.info("End Date ["+end+"]")
    }
    else if(currentDate.getDay() == 4)
    {
     end=new Date(currentDate.getFullYear(),currentDate.getMonth(),currentDate.getDate()-6)
    }
    else if(currentDate.getDay() == 3)
    {
     end=new Date(currentDate.getFullYear(),currentDate.getMonth(),currentDate.getDate()-5)
    }
    else if(currentDate.getDay() == 2)
    {
     end=new Date(currentDate.getFullYear(),currentDate.getMonth(),currentDate.getDate()-4)
    }
    else if(currentDate.getDay() == 1)
    {
     end=new Date(currentDate.getFullYear(),currentDate.getMonth(),currentDate.getDate()-3)
    }


    end=Date.parse(currentDate);
    log.info("End-Date["+end+"]")         
    query="taskCreateTimestamp:["+start+" TO "+end+"]";