簡體   English   中英

如何從選擇中獲取最舊的日期?

[英]How do i get the oldest date from a selection?

我有一個日期選擇:

2015-09-25T11:08:00
2015-09-25T11:42:00
2015-09-25T17:02:00
2015-09-25T17:02:00
2015-09-28T13:09:00

我如何從這個選擇中獲得最近的日期。 我試圖使用

@messagedaterows.max {|obj| @message_thread_ID }
2.0.0p353 :005 > test
 => [2015-09-28 17:36:51 +0200, 2015-09-27 17:36:51 +0200] 
2.0.0p353 :006 > test.sort
 => [2015-09-27 17:36:51 +0200, 2015-09-28 17:36:51 +0200] 
2.0.0p353 :007 > test.sort.first
 => 2015-09-27 17:36:51 +0200 
2.0.0p353 :008 > test.sort.last
 => 2015-09-28 17:36:51 +0200 

為此,您不需要復雜的邏輯。

簡單地:

dates.max # => 2015-09-28 13:09:00

即使它們是字符串,這也應該有效:

dates.max # => "2015-09-28T13:09:00"

最早的日期也是最小的日期 因此,我會寫這樣的東西:

dates = options['Messages'].map { |entry| entry["Date"] }
dates.min
#=> 2015-09-25T11:08:00

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM