Skip to content

Instantly share code, notes, and snippets.

@tigris
Created February 29, 2024 04:36
Show Gist options
  • Save tigris/29b9ca64c63a7a73ae0f8526fc698bf4 to your computer and use it in GitHub Desktop.
Save tigris/29b9ca64c63a7a73ae0f8526fc698bf4 to your computer and use it in GitHub Desktop.
Ruby DateTime#next_year on leap days
#!/usr/bin/env ruby
require 'date'
now = DateTime.now
yesterday = now - 1
puts "Now: #{now}"
puts "Yesterday: #{yesterday}"
puts "======"
one_year_from_now = now.next_year
one_year_from_yesterday = yesterday.next_year
puts "Now + 1.year: #{one_year_from_now}"
puts "Yesterday + 1.year: #{one_year_from_yesterday}"
puts "======"
puts "Equal? #{one_year_from_now == one_year_from_yesterday}"
Now: 2024-02-29T15:34:50+11:00
Yesterday: 2024-02-28T15:34:50+11:00
======
Now + 1.year: 2025-02-28T15:34:50+11:00
Yesterday + 1.year: 2025-02-28T15:34:50+11:00
======
Equal? true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment