Syed Asad Abbas Zaidi
Syed Asad Abbas Zaidi

Reputation: 1066

Convert datetime to format in ruby on rails

How could I convert the following date string into accepted format in ruby on rails

Thu, Aug 25 12:00 AM

Also I want global solution that could also go with the following formats including the above given:

August 25 2016

Upvotes: 3

Views: 7650

Answers (2)

ogelacinyc
ogelacinyc

Reputation: 1372

if variable is Time or Date class

[variable].strftime("%a, %B %d %T:%s %P")

[variable].strftime("%B %d %Y")

if you want to see more format directives

check it out http://apidock.com/ruby/DateTime/strftime

Upvotes: 4

Gupta
Gupta

Reputation: 10358

The most important one is the config.time_zone configuration in your config/application.rb file. ActiveRecord will help you convert from and to (which the documentation fails to explain) UTC and the time zone of your choice. This means that if all you’re doing is having users post times through a form and use Active Record to persist it you’re good to go.

Date.today
 => Thu, 25 Aug 2016 

PARSING

When parsing time information it’s important to never do it without specifying the time zone. The best way to do this is to use Time.zone.parse (which will use the time zone specified in config.time_zone) instead of just Time.parse (which will use the computer’s time zone).

Hope This help you!

Upvotes: 0

Related Questions