I'm learning Ruby on Rails and have dates with the format
20170802173300
witch is:
Mie, 02 Ago 2017 17:33:00 -0300.
How can I convert this to ?
2017-08-02 17:33:00 -0300
I'm learning Ruby on Rails and have dates with the format
20170802173300
witch is:
Mie, 02 Ago 2017 17:33:00 -0300.
How can I convert this to ?
2017-08-02 17:33:00 -0300
You can use strftime
:
datetime = 'Mie, 02 Ago 2017 17:33:00 -0300'
datetime.strftime('%Y-%m-%d %H:%m:%S %z')
# => 2017-08-02 17:33:00
You can format time anyway you like, in Ruby with strftime.
For format you want it'll be like this:
%Y-%m-%d %H:%M:%S %z
For example: Time.now.strftime('%Y-%m-%d %H:%M:%S %z')
require 'date'
date = '20170802173300'
formatted_date = DateTime.parse(date).to_s
# => "2017-08-02T17:33:00+00:00"
# and to be more explicit:
really_formated_date = DateTime.parse(date).strftime('%Y-%m-%d %H:%M:%S %Z')
# => "2017-08-02 17:33:00 +00:00"
Date now = new Date(); SimpleDateFormat format = new SimpleDateFormat("dd-MM-yy");