[英]Rails Date Format (To match YYYY-MM-DD HH-MM-SS)
When I do this: 当我这样做:
DateTime.now.to_s
I get: 2015-07-31T22:22:05+02:00 我得到:2015-07-31T22:22:05 + 02:00
But I need it in this format: 但我需要这种格式:
2015-07-31 22:22:05
How can I format it this way? 我怎样才能这样格式化?
Since this is a common thing to want, there is a shorthand method to do this. 由于这是一个常见的事情,因此有一种简便的方法可以做到这一点。
DateTime.now.to_s(:db) #=> 2015-07-31 22:22:05
http://api.rubyonrails.org/classes/DateTime.html#method-i-to_formatted_s http://api.rubyonrails.org/classes/DateTime.html#method-i-to_formatted_s
DateTime.now.strftime("%Y-%m-%d %I:%M:%S")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.