266
you are viewing a single comment's thread
view the rest of the comments
[-] Semi_Hemi_Demigod@lemmy.world 30 points 4 days ago

And the best part is the Ruby way accounts for leap years.

[-] deadbeef79000@lemmy.nz 20 points 4 days ago

Well, 365 * 10 certainly doesn't ;-)

[-] eager_eagle@lemmy.world 12 points 4 days ago

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.

[-] Diplomjodler3@lemmy.world 4 points 4 days ago* (last edited 4 days ago)

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?

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')
[-] mdhughes@lemmy.sdf.org 4 points 3 days ago

And what happens when you run that on Feb 29, 2024?

[-] eager_eagle@lemmy.world 2 points 3 days ago

datetime raises a ValueError when trying to create an invalid date

[-] eager_eagle@lemmy.world 2 points 3 days ago

or just this

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())
[-] pulsey@feddit.org 3 points 4 days ago

there is a simple fix to account for leap years, just add 10/4 days. /s

[-] Feathercrown@lemmy.world 2 points 3 days ago

Don't forget non-leap-days every 200 years!

[-] sexybenfranklin@ttrpg.network 4 points 3 days ago

Every 100 years but not every 400 years! Isn't it fun?

this post was submitted on 24 Aug 2025
266 points (100.0% liked)

Programmer Humor

37993 readers
87 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 6 years ago
MODERATORS