Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby on rails recursive sub-string

I have the string (actually it's a date and time) like this "20120207080000".

Is there any option to split the given string using any pattern matching technique or anything else?

i.e I need the output like this

output = ["2012", "02", "07", "08", "00", "00"]

Otherwise is it possible to convert the given string into date/time object?.

ruby version: 1.8.7

like image 780
Mr. Black Avatar asked Jan 18 '26 03:01

Mr. Black


2 Answers

"20120207080000".unpack('A4A2A2A2A2A2') #=> ["2012", "02", "07", "08", "00", "00"]
like image 91
steenslag Avatar answered Jan 19 '26 18:01

steenslag


Get a DateTime object like this:

require 'date'
DateTime.strptime('20120207080000', '%Y%m%d%H%M%S')

Please take care that you are using the right order of elements. I was just guessing.

like image 22
iltempo Avatar answered Jan 19 '26 20:01

iltempo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!