申請背景。
用戶撰寫測驗,其結果將在 5 年后過期。
我需要讓他們選擇在 3 年到 5 年后重寫。
我將expiry_date
其設定為自創建之日起 5 年。
我的問題
我將如何寫我的宣告
我有 ActiveSupport gem
expiry_date
是日期格式
if expiry_date < 3.years.ago && expiry_date > 5.years.ago
#Above is checking the date itself which is not right
我正在努力實作的目標。
expiry_date = Mon, 25 Mar 2024 #Was created on 25 Mar 2019 (Created_date not a field in db)
# Statement check's if it is past 3 years (from created) but less than 5 years(from created)
returns True
#Any where outside the range returns False
我的想法
需要得到今天的日期。然后查看它在 5 年時間范圍內的位置,以及它是否在 3 到 5 年之間。
它需要精確到當天
任何幫助或建議將不勝感激。
uj5u.com熱心網友回復:
據我了解,您有一個 test_date:
test_date = Date.new(2019,3,3)
你計算到期日期加上 5 年:
expiry_date = test_date 5.years
或者
expiry_date = test_date (5*365).days
現在您想知道它是否已過期:
today = Date.today
expired = today > expiry_date
或者您想知道它是否處于重寫期(3 年后最多 5 年):
is_in_rewrite_period = today.between?(test_date 3.years, expiry_date)
或者
is_in_rewrite_period = today.between?(expiry_date - 2.years, expiry_date)
希望這可以幫助...
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/507666.html
上一篇:更新物件陣列中的鍵值php