【问题标题】:how to calculate salary of last week in javascript?如何在javascript中计算上周的工资?
【发布时间】:2017-01-12 05:28:14
【问题描述】:

我正在尝试计算员工上周的工资。这意味着本周不应该包括在计算中。现在我有 -7 从当前日期检查我的数据。这是我的代码

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+"]";

我的目标是计算周一到周五之间的上周工资。我只用了-7 来检查我的数据。请帮帮我

【问题讨论】:

  • 您的问题是“如何在 JavaScript 中验证时间戳是星期一、星期二、星期三或其他日期?”在代码中,您要求计算结果中您想要的上周精确帽子的薪水
  • 抱歉含糊不清。我只需要计算上周的工资。 @意思
  • 所以最后你想要当前日期和从 7 开始的前一天,对吗?
  • 您想计算上周的数据范围吗?那你算工资吗?
  • @sam 你已经准备好获取当前日期和从当前日期起的第 7 天,那么你在哪里卡住了?

标签: javascript node.js jaggery-js


【解决方案1】:

查看此链接,您可以获得上周的开始日期和结束日期。 如果您的数据有时间戳,则将时间戳“00:00:00”附加到开始日期,将“23:59:59”附加到结束日期。

get last week in javascript

【讨论】:

    【解决方案2】:

    "(salary / (当月工作日)) * (上周工作日)" 上一行应该是你计算上周工资的公式。因此,您需要一种计算给定日期间隔的工作日的方法。

    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;
    }
    

    然后,给出日期并应用公式。

    【讨论】:

    • 我的目标是只计算上周的工资。如果今天是星期一,则不应包括当天。以及计算周一至周五的工资。
    【解决方案3】:

    我在 Jaggeryjs 中使用了以下代码,对我来说工作正常。我还没有测试,但得到了预期的结果。

     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+"]";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多