尝试使用javascript在rails中使用日期

时间:2015-10-15 13:53:49

标签: javascript ruby-on-rails ruby

问题是我有一个js来抓住leave_start和leave_end之间的日期和日期,例如leave_start = 15-OCT-15和leave_end = 19-OCT-15。我的脚本解释了这一点并吐出了这个开始日期和结束日期10-14-2015 10-15-2015 10-16-2015。任何帮助将不胜感激,以便当我的脚本运行时,完整日期将如下所示10-15-2015 10-16-2015 10-17-2015 10-18-2015,10-19-2015。

这是我的haml文本字段

%table.table.table-bordered.table-striped
  %th Leave Start:
  %th= f.text_field :leave_start, :as => :date, :type => :date, :label => false, :id => 'leave_start', :input_html => {:value => ''}


%table.table.table-bordered.table-striped
  %th Leave End:
  %th= f.text_field :leave_end, :as => :date, :type => :date, :label => false, :id => 'leave_end', :input_html => {:value => ''}


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

这是js

$(document).ready(function() {


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

    var l_s = Date.parse($('#leave_start').val());
    var l_e = Date.parse($('#leave_end').val());



    var start = l_s,
        end = l_e,
        currentDate = new Date(start),
        between = []
    ;

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

    var date_start = new Date();
    console.log(date('Y-m-d', date_start));

    var formated_dates = between.reduce(function(dates, the_date){
        dates.push(date('m-d-Y' + ' ' , the_date));
        return dates;
    }, []);
    $('#gdates').val(formated_dates.join("\n"));

});

});

0 个答案:

没有答案