本文介绍了分割日期数组以编写条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一系列日期,我需要确保这些日期中的年份都早于1900.

I have an array of dates and I need to ensure that the year within these dates are all sooner than 1900.

我的数组看起来像

[
    [0] "1980-10-03",
    [1] "1981-10-03",
    [2] "2001-10-03"
]

现在我已经能够做的事情对我来说很好,这是采用索引之一并将其拆分以执行我需要的事情.像这样

Now what I have been able to do and this works well for me is taking one of the index's and splitting them up to do what I need. Like so

splitted_birth_date = array.first.split("-")

哪个给我输出

[
    [0] "1980",
    [1] "10",
    [2] "03"
]

我要继续学习的地方

splitted_birth_date.first.to_i > 1900?

之类的东西能够进行编辑以及完成该任务所需的内容.

and so forth been able to make the edits and things that I need for making this task work out.

但是,我的问题是我无法遍历数组中的所有元素并将其拆分.

My problem though, is that I am having trouble looping through all of the elements within my array and splitting them.

我尝试过

array.each do |birth_date|
  birth_date.to_i
end

但是似乎什么也没发生.最终,我无法遍历数组中的记录,并且无法处理其中的必要部分.

But nothing seems to happen. Ultimately I'm having trouble looping through records in an array, and manipulating the necessary parts in them.

推荐答案

array = ["1980-10-03", "1981-10-03", "2001-10-03"]
yr = 1990

res = array.select{|d| Date.parse(d).year < yr}

这篇关于分割日期数组以编写条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 02:07