ruby - How do I split a string into separate components to parse into a DateTime? -
this question has answer here:
- how split string 2 parts after position 5 answers
i have string looks like:
20130518134721 yyyymmddhhmmss
this time of day. knowing format of string, , knowing year padded 4 digits , others padded 2 digits, how can split string can extract specific information such month, or hour?
you can use datetime.strptime
convert string
datetime
object. can like:
date = datetime.strptime("20130518134721", "%y%m%d%h%m%s") → datetime
after that, can access different methods of date
object in ruby hour
or mon
.
date.hour #=> 13 date.mon #=> 5
also, remember require 'date'
use datetime
object.
Comments
Post a Comment