简体   繁体   English

如何用我的JavaScript函数摆脱周末约会?

[英]How to get rid of weekend dates with my javascript function?

I have a javascript function that looks at my leave_start and leave_end datepickers then it figures out all the dates in between leave_start and leave_end. 我有一个javascript函数,它查看我的leaves_start和leaves_end日期选择器,然后计算出leaves_start和leaves_end之间的所有日期。 Which works great, but question is lets say someone picked 4/10/15 which is a Friday for leave_start then for leave_end picked 4/13/15 which is a Monday this would give me all of the dates like this 4/10/15, 4/11/15, 4/12/15, 4/13/15. 哪个效果很好,但问题是,假设某人选择了15年4月15日,这是一个为Leave_start的星期五,然后为Leave_end选择了4/13/15,这是一个星期一,这将给我所有像这样的日期4/10/15 ,2015年4月11日,2015年4月12日,2015年4月13日。 How can I get it to just show me the dates that aren't on the weekend which would just be 4/10/15, 4/13/15? 我如何才能仅显示非周末日期,即15年4月10日,15年4月13日? any help would be greatly appreciated! 任何帮助将不胜感激!

side note Using ruby on rails 4+ 旁注在轨道上使用红宝石4+

This is my app.js 这是我的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"));

    });
  });

Additional info 附加信息

This is my view 这是我的看法

.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;'

Change this block of code to suit... 更改此代码块以适合...

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

It checks the day of the week and ignores days that are 0 or 6 (Sunday is 0, Saturday is 6). 它检查星期几,并忽略为0或6的天(星期日为0,星期六为6)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM