Return smallest time step
01:42 07 May 2026

I am working on an automated workflow in R. Therefore, i work with time related data. For aggregation purposes i want to have a way for R to tell me the smallest timestep my dates do, fitting to the lubridate-package's time units (e.g. "year", "month", "week" etc.).

So when i have data like that:

dates_m <- c("2021-05-01", "2021-06-01", "2021-07-01")
dates_d <- c("2021-05-01", "2021-05-02", "2021-05-14")

I want the output to look like that:

cool_function(dates_m)
> "month"

cool_function(dates_d)
> "day"

I don't even have an example of what i tried, because i don't even have an approach on how to adress this. I haven't found a function inside the lubridate-package, that can do that and my approaches include a lot of if-conditions. So many, that i believe there must be a smarter way in doing it.

r date lubridate