javascript - 如何使用我的 javascript 函数摆脱周末约会?

标签 javascript ruby-on-rails ruby json ruby-on-rails-4

我有一个 javascript 函数,它查看leave_start 和leave_end 日期选择器,然后计算出leave_start 和leave_end 之间的所有日期。效果很好,但问题是,假设有人选择了 4/10/15,这是一个星期五,作为leave_start,然后为leave_end选择了4/13/15,这是一个星期一,这会给我所有像这样的日期 4/10/15 、2015 年 4 月 11 日、2015 年 4 月 12 日、2015 年 4 月 13 日。我怎样才能让它只显示不在周末的日期,即 2015 年 4 月 10 日、2015 年 4 月 13 日?任何帮助将不胜感激!

旁注在 Rails 4+ 上使用 ruby​​

这是我的 app.js

 $(document).ready(function() {


     $('.customSub').click(function() {

         var start = $('#leave_start').datepicker("getDate"),
             end = $('#leave_end').datepicker("getDate"),
             currentDate = new Date(start),
             between = []
         ;

         while (currentDate <= end) {
             between.push(new Date(currentDate));
             currentDate.setDate(currentDate.getDate() + 1);
         }
         var date_start = new Date();


         var formated_dates = between.reduce(function(dates, the_date){
             dates.push(date('m-d-Y' + ' ' , the_date));
             return dates;
         }, []);
         // Figures out dates in between leave_end and leave_start and puts the value of it in gdates text_field
         $('#gdates').val(formated_dates.join("\n"));

    });
  });

其他信息

这是我的观点

.row-fluid
   =simple_form_for @entry, :url => url_for(:controller => 'entry', :action => 'create'), :method => :post do |f|



     %table.table.table-bordered.table-striped{:style => 'table-layout:fixed; width:100% !important;'}
       %th.lt Leave Start:
       %td.lt= f.text_field :leave_start,  :label => false, :id => 'leave_start', :input_html => {:value => ''}
       %td.lt= f.error :leave_start, :class => 'er'

     %table.table.table-bordered.table-striped{:style => 'table-layout:fixed; width:100% !important;'}
       %th.lt Leave End:
       %td.lt= f.text_field :leave_end,  :label => false, :id => 'leave_end', :input_html => {:value => ''}
       %td.lt= f.error :leave_end, :class => 'er'


     %td.lt= f.text_field :range_days, :label => false, :id => 'range_days', :input_html => {:value => ''}
     %td.lt= f.text_field :full_range, :label => false, :id => 'gdates', :input_html => {:value => ''}


     %table.table.table-bordered.table-striped{:style => 'table-layout:fixed; width:100% !important;'}
     = f.button :submit, "Submit", :class => 'customSub', :style => 'margin-left:50px;'

最佳答案

更改此代码块以适应...

while (currentDate <= end) {
    if (currentDate.getDay() % 6 > 0) {
        between.push(new Date(currentDate));
    }
    currentDate.setDate(currentDate.getDate() + 1);
}

它检查星期几并忽略 0 或 6 的日期(星期日为 0,星期六为 6)。

关于javascript - 如何使用我的 javascript 函数摆脱周末约会?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29541596/

相关文章:

ruby-on-rails - 在 ruby​​ 中将字符串转换为可符号化

ruby-on-rails - 使用嵌套路由与 accepts_nested_attributes_for 有什么区别?

ruby - RVM 与 ruby​​ 的 native 安装

javascript - 为什么我的 js.erb 文件中没有工作链接

javascript - 在从 API 接收值之前调用 React 状态

javascript - jquery 输入元素取消选择文本事件?

javascript - 如何在我的 sprockets 预处理 js 文件(不是 View )中获取 escape_javascript 和其他助手?

ruby-on-rails - flash[ :notice] or flash. 通知?

javascript - 为什么在 ES6 react 类中需要绑定(bind)

javascript - Angular Directive(指令) : Adding ng-class directive at compile time on existing template element