Ruby-on-rails Rails - 从日期和时间戳中获取日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4701494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:06:44  来源:igfitidea点击:

Rails - get date from date & time stamp

ruby-on-rails

提问by timWhit

In my Rails database, I have a timestamp that looks like this 2010-12-22 18:21:46 UTC. I need a way to just get the date 2010-12-22from it without re-doing the database.

在我的 Rails 数据库中,我有一个看起来像这样的时间戳2010-12-22 18:21:46 UTC。我需要一种方法来从中获取日期2010-12-22,而无需重新创建数据库。

回答by Ved

Try time.to_dateto extract date component from your timestamp.

尝试time.to_date从时间戳中提取日期组件。

回答by David Sulc

To get the Stringyou're looking for

为了得到String你正在寻找的

your_date.strftime("%Y-%m-%d")

Not sure what you mean about modifying the database?

不知道你说的修改数据库是什么意思?

回答by Rohit Jain

To get the String you're looking for

获取您正在寻找的字符串

your_date.strftime("%F")

below link u can get more type of date. http://ruby-doc.org/core-2.2.0/Time.html#method-i-strftime

在下面的链接中,您可以获得更多类型的日期。 http://ruby-doc.org/core-2.2.0/Time.html#method-i-strftime