本文介绍了Javascript - 前一个工作日可变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个自动输入日期到文本字段的下拉列表。

I'm trying to create a drop down list that will automatically enter dates into text fields.

选项LBD目前返回昨天的日期,但是,我'喜欢它返回最后一个工作日的日期。所以如果今天是星期一,它将返回星期五的日期,如果今天是星期二,它将返回星期一的日期。所以这样等等。

The option "LBD" currently returns yesterday's date, however, I'd like it to return the date of the last business day. So that if today is Monday, it would return Friday's date, if today is Tuesday it would return Monday's date. So on and so forth.

这是我迄今为止所做的:

This is what I've got so far:

Javascript: p>

Javascript:

<script>
$(document).ready(function() {
$("#datetype option").filter(function() {
    return $(this).val() == $("#datepickstart").val();
    return $(this).val() == $("#datepickend").val();
}).attr('selected', true);
    var d = new Date();
var yesterday = (d.getMonth() + 1) + "/" + (d.getDate() - 1) + "/" + d.getFullYear();
var today = (d.getMonth() + 1) + "/" + d.getDate() + "/" + d.getFullYear();
var fdm = (d.getMonth() + 1) + '/01/' + d.getFullYear();
var fdy = '01/01/' + new Date().getFullYear();
$("#datetype").on("change", function() {
    var selectedVal = $(this).find("option:selected").attr("value");
    if (selectedVal == 'LBD') {
        $("#datepickstart").val((yesterday));
        $("#datepickend").val((yesterday));
    }
    if (selectedVal == 'MtD') {
        $("#datepickstart").val(fdm);
        $("#datepickend").val(today);
    }
    if (selectedVal == 'YtD') {
        $("#datepickstart").val(fdy);
        $("#datepickend").val(today);
    }
});
})
</script>

html:

<select id="datetype" name="datetype">
<option value="">Please select...</option>
<option value="LBD">LBD</option>
<option value="MtD">MtD</option>
<option value="YtD">YtD</option>
</select>

<input type="text" id="datepickstart" name="datepickstart" value="">
<input type="text" id="datepickend" name="datepickend" value="">


推荐答案

为了达到预期的效果,

To achieve your expected result, calculate day and reduce the days to get previous business day.

$(document).ready(function() {
  $("#datetype option").filter(function() {
    return $(this).val() == $("#datepickstart").val();
    return $(this).val() == $("#datepickend").val();
  }).attr('selected', true);

  var d = new Date();
  var bd = d.getDay();
  if (bd < 2) {
    if (bd == 0) {
      bd = 2;
    } else {
      bd = 3;
    }
  } else {
    bd = 1;
  }
  var today = (d.getMonth() + 1) + "/" + d.getDate() + "/" + d.getFullYear();
  var lbd = (d.getMonth() + 1) + "/" + (d.getDate() - bd) + "/" + d.getFullYear();
  var fdm = (d.getMonth() + 1) + '/01/' + d.getFullYear();

  var fdy = '01/01/' + new Date().getFullYear();

  $("#datetype").on("change", function() {
    var selectedVal = $(this).find("option:selected").attr("value");
    if (selectedVal == 'LBD') {
      $("#datepickstart").val(lbd);
      $("#datepickend").val(today);
    }
    if (selectedVal == 'MtD') {
      $("#datepickstart").val(fdm);
      $("#datepickend").val(today);
    }
    if (selectedVal == 'YtD') {
      $("#datepickstart").val(fdy);
      $("#datepickend").val(today);
    }

  });
});

Codepen-

Codepen-http://codepen.io/nagasai/pen/wWgQYE

LBD计算部分

  var bd = d.getDay();// get day from today's date which be between 0-6 (0 is Sunday and 6 is Saturday
  if (bd < 2) {
    if (bd == 0) {
      bd = 2;
    } else {
      bd = 3;
    }
  } else {
    bd = 1;
  }
 //variable lbd will get the last business day by reducing the calculated lbd from today's date
  var lbd = (d.getMonth() + 1) + "/" + (d.getDate() - bd) + "/" + d.getFullYear();

这篇关于Javascript - 前一个工作日可变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 15:30