I'm new to using Ruby and this tickled me pink
I'm new to using Ruby and this tickled me pink


I'm new to using Ruby and this tickled me pink
You're viewing a single thread.
And the best part is the Ruby way accounts for leap years.
Well, 365 * 10
certainly doesn't ;-)
I prefer the one on the left because it's evident it doesn't account for leap days, while I'd be questioning whether the one on the right does.
I'll give it a shot. Looks a bit kludgy and I've been typing this on my phone while sitting on the toilet. What am I doing with my life?
undefined
from datetime import datetime now = datetime.now() year = now.strftime('%Y') month = now.strftime('%m') day = now.strftime('%d') tenyearsago = datetime(year-10, month, day) print(tenyearsago.strftime('%d.%m.%Y')
And what happens when you run that on Feb 29, 2024?
datetime raises a ValueError when trying to create an invalid date
or just this
py
from datetime import datetime today = datetime.today() ten_years_ago = today.replace(year=today.year - 10) print("Date 10 years ago:", ten_years_ago.date())
there is a simple fix to account for leap years, just add 10/4 days. /s
Don't forget non-leap-days every 200 years!
Every 100 years but not every 400 years! Isn't it fun?