Is there a way to represent dates like 12/25 without year information? I’m thinking of just using an array of [month, year] unless there is a better way.
Is there a way to represent dates like 12/25 without year information? I’m thinking
Share
You could use the Date class and hard set the year to a leap year (so that you could represent 2/29 if you wanted). This would be convenient if you needed to perform ‘distance’ calculations between two dates (assuming that you didn’t need to wrap across year boundaries and that you didn’t care about the off-by-one day answers you’d get when crossing 2/29 incorrectly for some years).
It might also be convenient because you could use
#strftimeto display the date as (for example) “Mar-3” if you wanted.Depending on the usage, though, I think I would probably represent them explicitly, either in a paired array or something like
YearlessDate = Struct.new(:month,:day). That way you’re not tempted to make mistakes like those mentioned above.However, I’ve never had a date that wasn’t actually associated with a year. Assuming this is the case for you, then @SeanHill’s answer is best: keep the year info but don’t display it to the user when it’s not appropriate.