简体   繁体   English

根据DateTime搜索

[英]Search based on DateTime

I'm relatively new to ruby on rails. 我是刚接触红宝石的人。 I do have a custom search form where I want to search with date and time. 我有一个自定义搜索表单,我想在其中搜索日期和时间。 My database table does have a column with datetime as a datatype. 我的数据库表确实有一列,其中datetime为数据类型。 I knew that we can extract date from Date(column name). 我知道我们可以从Date(列名)中提取日期。 I would like to know how to extract only time from datetime field. 我想知道如何仅从datetime字段中提取时间。 Please help me with this. 请帮我解决一下这个。 Thanks in advance 提前致谢

Use date_format 使用date_format

mysql> select id, created_at from users limit 1;
+----+---------------------+
| id | created_at          |
+----+---------------------+
|  8 | 2009-08-19 09:24:24 |
+----+---------------------+
1 row in set (0.00 sec)

mysql> select id, DATE_FORMAT(created_at, "%h/%i/%s") from users limit 1;
+----+-------------------------------------+
| id | DATE_FORMAT(created_at, "%h/%i/%s") |
+----+-------------------------------------+
|  8 | 09/24/24                            |

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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