简体   繁体   中英

Rails DateTime date from string -> invalid date

I'm currently working on a rails project and I need to create a DateTime object from a string.

Currently I have the following code:

datetime = DateTime.strptime(items[0] << 'T' << items[1], '%d.%m.%YT%H:%M:%S')

The string created by items[0] << 'T' << items[1] is for example 25.01.2012T18:25:47

But the I get the following error message "invalid date"

If I print the string to the rails server console it seems to be correct and if I replace "items[0] << 'T' << items[1]" with a valid string in the strptime method (eg 25.01.2012T18:25:47) it works, but I have no idea why the conversion to a DateTime object fails if i use the concatinated string...

Does anyone got an idea?

Best regards

You can try (items[0] << 'T' << items[1]).to_datetime . It will give an object in DateTime format.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM