O
O
Ord EO2019-09-08 20:34:18
ruby
Ord EO, 2019-09-08 20:34:18

How to extract the year into a variable from a full date?

I get a date from the database in this form 2019-07-10 00:18:50 +0300,, how can I save only the year in a variable using only pure Ruby?
Does this require regular expressions?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
DevMan, 2019-09-09
@OrdeO

no Time class or regular is needed - just take the first 4 characters of the string.

G
GeneAYak, 2019-09-08
@GeneAYak

Ruby has a special Time class
. Just call time = Time.parse("2019-07-10 00:18:50 +0300")
You can already extract anything from the time variable, for example, time.year

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question