DateTime parsing not working properly

I have a Ruby code that does not look like this.

str = 2010-12-02_12-10-26 puts str puts DateTime.parse(str, "%Y-%m-%d_%H-%M-%S") 

I expected to get the actual time from parsing. Instead, I get the output as follows:

 2010-12-02_12-10-26 2010-12-02T00:00:00+00:00 

How to get the time in which you analyzed?

+6
ruby datetime parsing
source share
2 answers

It works:

 str = "2010-12-02_12-10-26" puts str puts DateTime.strptime(str, "%Y-%m-%d_%H-%M-%S") 

This example is on Codepad .

According to the parse documentation :

Create a new DateTime object by parsing from a string without specifying a format.

And strptime :

Create a new DateTime object by parsing from a string according to the specified format.

+13
source share

Use strptime instead of parse

 puts DateTime.strptime(str, "%Y-%m-%d_%H-%M-%S") 
+1
source share

All Articles