1

I have timestamp as "1432927800000"

I want to save it in DateTime column of MySQL. (SQL DateTime column stores date in this format : 2010-06-24 11:30:00)

I have tried

uTime = params[:fromDate] #contains "1432927800000"
Date.new(uTime).to_formatted_s(:db) #Comparison of String with 0 failed.
DateTime.strptime("1318996912",'%s') #Give weird date: Wed, 03 Sep 47377 12:00:00 +0000
Date.parse(uTime).to_s(:db) #Invalid Date

I am expecting 2015-5-30 1:00:00 from "1432927800000"

2
  • I have added the answer please check. Commented May 30, 2015 at 15:22
  • your expected resul is "2015-05-30 01:00:00" but .to_s(:db) is returns "2015-05-29 22:30:00" Commented May 30, 2015 at 20:47

2 Answers 2

4

Instead of strftime will be better to use predefined :db pattern from ActiveSupport:

2.1.0 :002 > Time.at(1432927800000/1000).to_s(:db)
 => "2015-05-29 22:30:00"

to_s is alias for to_formatted_s method and defined in Time and DateTime classes.

Sign up to request clarification or add additional context in comments.

3 Comments

webDev's expected resul is "2015-05-30 01:00:00" but .to_s(:db) is returns "2015-05-29 22:30:00"
@Amit this solutions looks like something which will work for multiple Databases... isn't it? I need to see why this returns time as 22:30:00 instead 01:00:00
I found some difference in what ActiveSupport returns and what sql returns. When In sql it is 2018-07-16 12:01:16.234121 but with to_s(:db) method to_s(:db) it changes to 2018-07-16 12:01:16. Why the fraction is not coming with to_s(:db)
1

Try this I Hope this will help you

I have tried it in my Rails console and I got following.

uTime = 1432927800000

Time.at(uTime/1000).strftime("%Y-%m-%d %I:%M:%S")

## Output

"2015-05-30 01:00:00"

This a link where you which will helps you to convert unix timestamp http://www.epochconverter.com/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.