I've got a time with value Wed, 23 Mar 2016 00:07:00 ICT +07:00, and when calling to_s, it returns me "03/23/2016 12:07 AM", but what I need is "03/23/2016 07:07 AM".
Appreciate an answer with explanation.
If you ever need a time in a specific format, you should reach for strftime. The format string you want is '%m/%d/%Y %H:%M %p', the manual I linked to above explains what all the format specifiers mean. Anything that is time-ish in Ruby or Rails is going to have a strftime method that matches Time#strftime so it doesn't matter if you have a Time, DateTime, ActiveSupport::TimeWithZone, ...
You also appear to want to convert the time to UTC but there's a method for that too: utc.
Putting those together:
t.utc.strftime('%m/%d/%Y %H:%M %p')
You really shouldn't use to_s or inspect on times for anything other than debugging. You're better off going straight to strftime so that you will get exactly the format you're after.
I guess this would work:
(t + (7*60*60)).to_s where t is an instance of Time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With