2017-02-02 62 views
2

在我的rails項目中,我只希望在一天之內獲得Date實例。年和月使用當前值。如何在一天之內創建新的日期實例

我可以寫這樣的:

day = 3 
date = Date.new(Date.current.year, Date.current.month, day) 

date = Date.current.beginning_of_month + (day - 1).days 

你會如何寫函數這樣嗎? 有更好的實現嗎?

回答

1

如果你已經有了一個Date對象,你可以這樣做:

date.change(day: 3) 

,其中日期是日期或日期時間對象。你也可以這樣做:

Date.today.change(day: 3) 
相關問題