简体   繁体   中英

strptime and I18n, how to parse a date for current language

I'm using strptime to parse a date formatted in dd/mm/YYYY format, however I just thought that simply parsing in this way will break i18n support, because if my customer is using a different language I will try to parse it in my format, while I want it to use date.formats.default format in that function.

How to achieve this?

我用这种方式解决了我的问题:

Date.strptime(params[:date], I18n.translate('date.formats.default'))

If you are using Date or Time you can use .to_s to apply default format

Date.today.to_s
Time.now.to_s

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