Caleb Codes

How to Tell if Today Is the First of the Week/Month/Year With Ruby

I had a project recently where a cron would fire early in the morning and generate stats for the previous day/week/month/year. I needed to know when it fired if today was the beginning of a new month so I could generate last month’s stats.

1
2
3
4
5
6
7
8
9
10
11
#First hour of the day:
Time.now.hour == 0

#Beginnning of the week
Time.now.beginning_of_week == Time.now.beginning_of_hour

#First day of the month 
Time.now.day == 1 && Time.now.hour == 0

#First day of the year
Time.now.beginning_of_year == Time.now.beginning_of_hour

Comments