2013-08-06 24 views
1

我嘗試使用設置的Google Analytics開始日期和結束日期來捕獲過去30天內的唯一身份訪問者。我製作了end_date = DateTime.now.strftime("%Y-%m-%d"),我如何設置start_date到30天前。如何使用DateTime.now.strftime(「%Y-%m-%d」)將日期時間設置爲1個月前?

+0

我們需要看到示例代碼,顯示您嘗試過的內容。見http://sscce.org/ –

+0

問題不是30天。任何解決方案,我想知道? – Topka

回答

1

問題是,你與你的end_date創作過猶不及。

你正在把它變成一個沒有數學能力的字符串。相反,把它作爲一個DateTime,你繼承做日期的功能的能力,add and subtract integers

require 'date' 

datetime_now = DateTime.now 
end_date = datetime_now.strftime("%Y-%m-%d") # => "2013-08-06" 
end_date.class # => String 

end_date = datetime_now # => #<DateTime: 2013-08-06T14:55:20-07:00 ((2456511j,78920s,731393000n),-25200s,2299161j)> 
end_date - 30 # => #<DateTime: 2013-07-07T14:55:20-07:00 ((2456481j,78920s,731393000n),-25200s,2299161j)> 

注意end_date - 30返回datetime這正是前面30天;時間組件被保留。一旦你有你想要的值,將該值轉換爲一個字符串。

0

每鐵皮人的建議更新時間:

在Ruby:

require 'date' 
end_date = DateTime.now 
start_date = end_date - 30 
formatted_end_date = end_date.strftime("%Y-%m-%d") 
formatted_start_date = start_date.strftime("%Y-%m-%d") 

在Rails或者active_support/time要求:

使用天前方法:

start_date = 30.days.ago 

這裏是代碼:

end_date = DateTime.now 
start_date = end_date - 30.days 
formatted_end_date = end_date.strftime("%Y-%m-%d") 
formatted_start_date = start_date.strftime("%Y-%m-%d") 

這種方式start_date恰好是在end_date之前30天。如果您實例化兩個DateTime,它們將不會完全相隔30天。

daysago方法可以在Rails環境這一行的外部訪問:

require 'active_support/time' 
+0

你可能想解釋'days'和'ago'的來源以及如何包含它們。 OP使用直接的Ruby,所以這些不可用。 –

+0

@theTinMan - 我同意你的評論,並相應地更新了我的答案。謝謝。 – Powers

+0

不,不,不建議加載整個Active Support代碼庫,只是爲了挑選一些需要的方法。使用[Date]的核心擴展(http://guides.rubyonrails.org/active_support_core_extensions.html#extensions-to-date),[DateTime](http://guides.rubyonrails.org/active_support_core_extensions.html#extensions-日期時間)和[時間](http://guides.rubyonrails.org/active_support_core_extensions.html#extensions-to-time)。事實上,閱讀「[如何加載核心擴展](http://guides.rubyonrails.org/active_support_core_extensions.html#how-to-load-core-extensions)」。 –

2

有了基本的Ruby中,你可以這樣做:

> irb 
require 'date' # needed 
today_minus_30 = Date.today.to_date - 30 

# if you need the date as a string 
date_as_string = today_minutes_30.strftime("%Y-%m-%d") 
4

我意識到這個問題已經很老了,但這裏有一個乾淨的方式來做到這一點的人誰需要知道:

start_date = (Date.now - 30.days).strftime("%Y-%m-%d") 

start_date = (Date.now - 1.month).strftime("%Y-%m-%d") 

你可以做類似的事情DateTime.now,並Time.now

0

我會建議使用輔助方法。類似於

# Gets time range for x number timeunits ago 
    def time_range(unit, timeunit = nil) 
    if timeunit == "weeks" 
     now = Time.zone.now.beginning_of_week 
    elsif timeunit == "months" 
     now = Time.zone.now.beginning_of_month 
    else 
     now = Time.zone.now.beginning_of_day 
    end 
    # Ex: time_range(0, "days") --> Get the time range for today between the beginning of today and the beginning of tommorow - 1 second 
    now - unit.send(timeunit)..now + 1.send(timeunit) - 1.seconds - unit.send(timeunit) 
    end 

將幫助您請求時間範圍。所以當你請求類似的東西時;

time_range(0, "months") 

它會返回0個月前(本月)的時間範圍;

Thu, 01 Sep 2016 00:00:00 UTC +00:00..Fri, 30 Sep 2016 23:59:59 UTC +00:00 

然後你可以簡單地查詢數據庫對象並計算範圍內的所有內容;

Visit.where(started_at: time_range(unit, timeunit)).count 
相關問題