Ruby Datetime parse
I am trying to compare strings by date. First, I store the data with the date string like this:
2/10/15 23:37
to my PostgreSQL database.
I can see that my date was saved and now looks like this: 2015-02-10 23:37:00
and the locale is set to lc_collate = en_US.UTF-8
.
Now I am using the same file and I want to compare the date field to see if it is equal (and it should be!), But Ruby reads 2/10/15 23:37
like 0002-10-15 13:01:00 +0800
.
I tried to do this too:
Date.strptime(myDate, "%m/%d/%Y")
But the result was 0015-02-12
.
How can I get Ruby to read my input line the same way as my DB? I tried to install config.i18n.default_locale = 'en-US'
.rb in my application but it didn't help.
Since you are supplying a two-digit year, it should be:
Date.strptime(myDate, "%m/%d/%y") # Note the lower case y
If timing is also important, you should use:
DateTime.strptime(myDate, "%m/%d/%y %H:%M")
You can do it:
starting_time = Time.now - 1.hour
starting_time.to_s(:db)
This will result in what starting_time
in my example matches what you find in the DATETIME field of the database.