views:

369

answers:

5

Usage of Time.now or Time.now.to_s or Time.now.to_datetime is throwing error.

Usage of Date.today doesn't help as it stores the date with time as 12.00 AM and not the current time stamp.

Please help me in resolving this issue.

+1  A: 

Did you try a statement of the kind (with now() in the update statement):

INSERT INTO table1 (field1, field2, field3, timestampfield) VALUES ('value1', 'value2', 'value3', now())


[EDIT:] Have a look here for an explanation of "now()"

Peter
+1  A: 

For update try something like:

UPDATE table1 SET mytime = now();

Another option for you if you want to put proper time on insert, it is just set function now() for the datetime field default.

Dmitris
I used CURRENT_TIMESTAMP to get the current date with timestamp. It worked.
+2  A: 

I'm not sure why and what is throwing an error, but if you're trying to generate a date string that is correct SQL from Ruby (specifically Rails here) you can use Time.now.to_s(:db).

ujh
I think that you should seriously contemplate this answer as compared with the others. Maybe you want your timestamping to be a function of the clock that the database is running on, but maybe you want to have the timestamping be determined by the system executing the code. Only this answer will provide that functionality.
animal
+1  A: 

I used CURRENT_TIMESTAMP to get the current date with timestamp. It worked.

thanks for your help.

A: 

Time.now.to_formatted_s :db this should give you db friendly time stamp.

http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Time/Conversions.html#M001093

Dipesh Batheja