Datetime string format for ActiveRecord fixture (non-Rails)

David Moles

Rails apparently provides an extension to Time.to_s allows you format the timestamp in a database-friendly way, e.g. Time.now.to_s(:db). This can be used in an ActiveRecord dynamic fixture:

my_record:
  some_datetime: <%= Time.utc(2015, 1, 1).to_s(:db) %>

However, outside Rails, this fails with

ArgumentError: wrong number of arguments (1 for 0)
  (...) in `to_s'

If I'm going to convert the time by hand, what is the format (with time zone etc.) I should be using here?

Alternatively, if I'm wrong and it's not Rails but something in ActiveRecord that adds this extension, what do I need to require to get it?

aaron-coding

The format is like this: YYYY-MM-DD HH-MM-SS

Another way to get that format is with strftime:

DateTime.now.strftime('%Y-%m-%d %I:%M:%S')

If you want the include timezone, add %z:

DateTime.now.strftime('%Y-%m-%d %I:%M:%S %z')

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Displaying string with format in ActiveRecord

From Dev

Rails Activerecord/Postgres time format

From Dev

Rails I18n: datetime format inside translated string

From Dev

Rails I18n: datetime format inside translated string

From Dev

Format datetime in model in rails

From Dev

Format datetime in model in rails

From Dev

How to format string to datetime?

From Dev

Datetime string format alignment

From Dev

Accurate DateTime string format

From Dev

Format datetime string in jsp?

From Dev

Convert String to DateTime Format

From Dev

SQL datetime to string format

From Dev

Convert string to DateTime and format

From Dev

Non-activerecord models structure in Rails 4

From Dev

How to Stub Non-ActiveRecord Models in Rails?

From Dev

Rake test error - ActiveRecord::Fixture::FormatError: "Ruby on Rails Tutorial" by MICHAEL HARTL

From Dev

ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError while testing

From Dev

Powershell Converting datetime object to string - How to display time in non-military format?

From Dev

How to convert string to datetime(datetime) in same format?

From Dev

Convert datetime string in datetime format in SQL server

From Dev

Ruby on Rails invalid date error when parsing datetime string to convert to a different format

From Dev

DateTime.now.strftime('Y%m%d%H%M%S') proper string format in Rails

From Dev

DateTime.now.strftime('Y%m%d%H%M%S') proper string format in Rails

From Dev

Convert a string with 'YYYYMMDDHHMMSS' format to datetime

From Dev

DateTime string format in c#

From Dev

How to check datetime format in a string?

From Dev

Convert a string Date to the DateTime Format?

From Dev

Format DateTime without string conversion

From Dev

Converting a String into a Valid DateTime Format

Related Related

  1. 1

    Displaying string with format in ActiveRecord

  2. 2

    Rails Activerecord/Postgres time format

  3. 3

    Rails I18n: datetime format inside translated string

  4. 4

    Rails I18n: datetime format inside translated string

  5. 5

    Format datetime in model in rails

  6. 6

    Format datetime in model in rails

  7. 7

    How to format string to datetime?

  8. 8

    Datetime string format alignment

  9. 9

    Accurate DateTime string format

  10. 10

    Format datetime string in jsp?

  11. 11

    Convert String to DateTime Format

  12. 12

    SQL datetime to string format

  13. 13

    Convert string to DateTime and format

  14. 14

    Non-activerecord models structure in Rails 4

  15. 15

    How to Stub Non-ActiveRecord Models in Rails?

  16. 16

    Rake test error - ActiveRecord::Fixture::FormatError: "Ruby on Rails Tutorial" by MICHAEL HARTL

  17. 17

    ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError while testing

  18. 18

    Powershell Converting datetime object to string - How to display time in non-military format?

  19. 19

    How to convert string to datetime(datetime) in same format?

  20. 20

    Convert datetime string in datetime format in SQL server

  21. 21

    Ruby on Rails invalid date error when parsing datetime string to convert to a different format

  22. 22

    DateTime.now.strftime('Y%m%d%H%M%S') proper string format in Rails

  23. 23

    DateTime.now.strftime('Y%m%d%H%M%S') proper string format in Rails

  24. 24

    Convert a string with 'YYYYMMDDHHMMSS' format to datetime

  25. 25

    DateTime string format in c#

  26. 26

    How to check datetime format in a string?

  27. 27

    Convert a string Date to the DateTime Format?

  28. 28

    Format DateTime without string conversion

  29. 29

    Converting a String into a Valid DateTime Format

HotTag

Archive