9人参与 • 2025-03-03 • Mysql
date_format()
是mysql中用于格式化日期时间的函数:
语法:
date_format(date, format_string)
date
:需要格式化的日期化时间值,一般是需要被格式化的日期时间类型(datetime类),但也可以是日期时间形式的字符串format_string
:格式化字符串,用于指定日期时间的格式化输出形式注1:mysql允许你对具体的日期时间格式的字符串进行格式化,但其他数据库的格式化函数不一定支持,比如clickhouse的formatdatetime()
注2:mysql对字符串进行格式化时,日期时间的格式并不固定,你可以用很多特殊字符隔开日期时间,比如: /
*
-
<
等,但不能是空格,mysql会按照yy mm dd hh mm ss
的顺序识别字符串中的数字
-- 比如下列sql语句的执行结果相同,都是 "2024,december,31st" select date_format( '24-12-31', '%y,%m,%d') as format_date select date_format( '2024-12-31', '%y,%m,%d') as format_date select date_format( '2024/12/31', '%y,%m,%d') as format_date select date_format( '2024*12*31', '%y,%m,%d') as format_date select date_format( '24<<12<<31', '%y,%m,%d') as format_date -- 但如果使用空格间隔,会执行失败,但不会报错,输出结果为null select date_format( '24 12 31', '%y,%m,%d') as format_date
格式 | 含义 |
---|---|
%y | 年份:4位数字表示,eg: 2024 |
%y | 年份:2位数字表示,eg: 24 |
%m | 月份:英文全拼表示,eg: june |
%m | 月份:2位数字表示,范围01-12,eg: 06 |
%d | 天数:两位数字表示,范围01-31,eg: 15 |
%d | 天数:英文第几天表示,范围 1st-31th,eg: 1st, 2nd, 3rd, 15th |
%h | 小时:两位数字表示,范围00-23,eg: 23 |
%i | 分钟:两位数字表示,范围00-59,eg: 59 |
%s or %s | 秒钟:两位数字表示,范围00-59,eg: 16 |
%w | 星期:英文全拼表示,eg: saturday |
%w | 星期:1位数字表示,eg: 6 |
%t | 完全时间格式,相当于 %h:%i%s 或 %h:%i:%s,范围00:00:00-23:59:59,eg: 15:30:36 |
注3:大小写形式%i 和%t无意义
常见的日期时间格式化形式基本上就是如下几种:%y,%m,%d
%y-%m-%d %h:%i:%s
%y/%m/%d
%w,%m,%d
sql语句
select '%y-%m-%d %h:%i:%s' as format, date_format( '2024-12-31 23:48:59','%y-%m-%d %h:%i:%s') as format_date union all select '%y,%m,%d' as format, date_format( '2024-12-31 23:48:59','%y,%m,%d') as format_date union all select '%w,%m,%d' as format, date_format( '2024-12-31 23:48:59','%w,%m,%d') as format_date union all select '%y/%m/%d' as format, date_format( '2024-12-31 23:48:59','%y/%m/%d') as format_date union all select '%h:%i:%s' as format, date_format( '2024-12-31 23:48:59','%h:%i:%s') as format_date union all select '%h:%i:%s' as format, date_format( '2024-12-31 23:48:59','%h:%i:%s') as format_date union all select '%t' as format, date_format( '2024-12-31 23:48:59','%t') as format_date union all select '%w,%m-%d' as format, date_format( '2024-12-31 23:48:59','%w,%m-%d') as format_date union all select '------' as format, '--------------' as format_date union all select '%h:%i:%s' as format, date_format( '2024-12-31 23:48:59','%h:%i:%s') as format_date union all select '%t' as format, date_format( '2024-12-31 23:48:59','%t') as format_date
执行结果
在实际的业务应用如报表导出、界面显示、数据分析等,该函数通常用于输出特定格式的日期或时间,当然,date_format()
允许你自定义输出格式,只要你定义好格式化字符串即可。
select date_format(starttime,'%y-%m') as starttime from tablea where column1 = 'option1'
date_format()
是mysql中用于格式化输出日期时间的函数,我们可以通过使用该函数满足大多数格式化日期时间的应用场景,本文的示例展示了如何获取当前日期和时间并将其格式化为一个常见的格式,您可以根据需要调整格式字符串来满足您的具体需求。
但需要注意的是,不同的数据库系统可能支持不同的格式字符串,所以在使用时需要参考相应数据库的文档来确定正确的格式字符串。
到此这篇关于mysql 日期时间格式化函数 date_format() 的使用详解的文章就介绍到这了,更多相关mysql 日期时间格式化内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论