0

I have used the Time.new method in my rails application to get the current date and I want to store this date into a variable of type datetime . I am using postgresql. Now if I insert the date of this format manually into table using pgAdminIII ,it is working fine but if I try to insert the time into the table via a controller code then it is storing a different date time into table.

For example suppose the current time is:-

2016-06-15 17:29:29

And if I insert this date and time into the table manually by copying this line from webpage then it is working fine.

But my controller code to store it into table is inserting something else like

2016-06-15 11:41:39

Note: I have used only Time.new (time region is not specified in my code)

Abhradip
  • 393
  • 5
  • 27
  • 1
    Rails stores datetime converted to UTC in the database. So I don't see an issue here. – spickermann Jun 15 '16 at 12:23
  • share some code and what you would like to see final result. – 7urkm3n Jun 15 '16 at 12:33
  • please check your timezone in application.rb config.time_zone = 'your timezone'. The time value is storage in database as you specify timezone in that. – Kinjal Shah Jun 15 '16 at 12:50
  • But I need to convert it into UTC too when displaying on webpage. It is displaying time in webpage in IST format but I need UTC – Abhradip Jun 16 '16 at 05:32

1 Answers1

0

It is just that the active record is using the utc as a timezone. If you want Active Record to store your time in the preferred timezone, change here

# application.rb
config.time_zone = 'Eastern Time (US & Canada)' //insert your time zone here
config.active_record.default_timezone = :local

reference: How to change default timezone for Active Record in Rails?

Community
  • 1
  • 1
Milan Pudasaini
  • 594
  • 3
  • 12