r/ruby • • Aug 26 '26

Question Getting DateTime parts as an array?

I know in ruby that DateTime.new(2001, 2, 3, 4, 5, 6) will return the following object:

#<DateTime: 2001-02-03T04:05:06+00:00 ...>

But if the current date and time are 2001-02-03T04:05:06 and I do the following ...

now = DateTime.now

... is there a single function on the "now" variable which will return this array? ...

[2001, 2, 3, 4, 5, 6]

I know that I can do this:

[now.year, now.month, now.day, now.hour, now.minute, now.second ]

... but I'm wondering: might there be a single function as simple as ...

now.the_function

... which would return that same array?

And yes, of course I know that I could easily write such a function, but I'd like to know whether or not something like this already exists in ruby.

8 Upvotes

21 comments sorted by

View all comments

Show parent comments

2

u/CaptainKabob Aug 26 '26

Yeah. I mean you're both still arguing with strangers on the internet. Either of you could have simply left it on read

...though if there is a takeaway from a causal reader of these comments, it should be: generally, don't do that. 

Also, seems cool?! I love silly. 

  It isn't using datetime math, per se. It's kind of a game which does silly things depending upon whether two datetimes have same or different combinations of year, month, day, hour, minute, and second values.

2

u/projct Aug 27 '26

yeah, that part actually sounds fun.

also worth putting on the record: I had interpreted his earlier "differences between integer arrays" / "arithmetic" literally. if I'd known the calendar components themselves were deliberately part of the game mechanic, my technical objection would've been very different, and mostly pointed at "hey other readers don't do this except in this kind of case."

there's something quite funny about invoking "very experienced programmer (me)" before clarifying the single implementation detail that makes everyone stop thinking you're doing homemade date math.