Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby Datetime parse

I am trying to compare rows by dates. First, I am saving data with string date's like this:

2/10/15 23:37 

into my PostgreSQL DB.

I can see that my date was saved and now looks like this: 2015-02-10 23:37:00, and the locale field 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's the same (and it should be!), but Ruby reads 2/10/15 23:37 as 0002-10-15 13:01:00 +0800.

I was trying also this:

Date.strptime(myDate, "%m/%d/%Y")

But the result was 0015-02-12.

How can I force Ruby to read my input string the same way as my DB? I was trying to set config.i18n.default_locale = 'en-US' in my application.rb but it didn't help.

like image 440
Krzysztof Witczak Avatar asked Aug 19 '26 11:08

Krzysztof Witczak


1 Answers

Since you're supplying a two digit year, it should be:

Date.strptime(myDate, "%m/%d/%y") # Note the lower case y

If the time is also important, you should use:

DateTime.strptime(myDate, "%m/%d/%y %H:%M")
like image 78
Mischa Avatar answered Aug 22 '26 09:08

Mischa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!