当前位置:网站首页> MySQL中的日期时间类型与格式化方式
MySQL中的日期时间类型与格式化方式
2022-07-04 18:39:00 【1024问】
【1】MySQL中的日期时间类型
① 详细解释
② SQL语句实例
③ timestamp字段
④ 测试实例
【2】日期时间类型格式化 ① DATE_FORMAT( )函数
② date_format( ) 转换格式
③ str_to_date()函数
【1】MySQL中的日期时间类型MySQL中常用的几种时间类型有:date、datetime、time、year、timestamp
date | 4 | 1000-01-01 | 9999-12-31 | 0000-00-00 |
datetime | 8 | 1000-01-01 00:00:00 | 9999-12-31 23:59:59 | 0000-00-00 00:00: 00 |
timestamp | 4 | 19700101080001 | 2038年的某个时刻 | 0000000000000000 |
time | 3 | -838:59:59 | 838:59:59 | 00:00:00 |
year | 1 | 1901 | 2155 | 0000 |
datetime
: 时间日期型,格式是YYYY-mm-dd HH:ii:ss,表示的范围是从1000到9999。但是有零值,0000-00-00 00:00:00;
date
:日期,就是datetime中的date部分;
time
:时间(段),指定的某个区间之间,从-时间到+时间(有负时间表示);
timestamp
:时间戳,并不是常规意义时间戳(如:14253685),范围是’1970-01-01 00:00:00’到2037年。格式为YYYY-mm-dd HH:ii:ss,格式与datetime完全一致;
year
:yy和yyyy,yyyy的范围是1901-2155,yy的范围是1970-2069。
两位year(00-69表示2000-2069,70-99表示1970~1999)。当应用只需要记录年份时,year比date更省空间
② SQL语句实例create table my_date(d1 datetime,d2 date,d3 time,d4 timestamp,d5 year)charset utf8;desc my_date
如下图所示:year默认为4位,即YYYY; timestamp不能为空,有默认值,在创建新记录和修改现有记录的时候都对这个数据列刷新。
如下分别插入几条数据并对time做差异分析:
insert into my_date VALUES('2015-09-28 11:50:36','2015-09-28','11:50:54','2015-09-28 11:51:08','2015');insert into my_date VALUES('2015-09-28 11:50:36','2015-09-28','-11:50:54','2015-09-28 11:51:08','2015');-- -11insert into my_date VALUES('2015-09-28 11:50:36','2015-09-28','-211:50:54','2015-09-28 11:51:08','2015');-- -2 11insert into my_date VALUES('2015-09-28 11:50:36','2015-09-28','-2 11:50:54','2015-09-28 11:51:08','2015');-- -2过去两天#year用69标识-2069insert into my_date VALUES('2015-09-28 11:50:36','2015-09-28','11:50:54','2015-09-28 11:51:08','69');-- 69#year用70标识-1970insert into my_date VALUES('2015-09-28 11:50:36','2015-09-28','11:50:54','2015-09-28 11:51:08','70');-- 70
默认情况下只要当前所在的记录被更新,该字段一定会自动更新成当前时间。
update my_date set d1 = SYSDATE() where d5=69;select * from my_date
那么MySQL可以拿到真正的时间戳吗?当然可以!
select UNIX_TIMESTAMP();
4.1 查询当前时间
SELECT SYSDATE() from dual;
4.2 将当前时间插入以上几种类型列中
insert INTO `user` (name,number,date,datetime,timestamp,time,year)VALUES ('Loum',3,SYSDATE(),SYSDATE(),SYSDATE(),SYSDATE(),2016);
4.3 mysql中datetime类型的长度位数
如下所示,通常我们MySQL中设计datetime类型长度都默认为0:
`work_time` datetime(0) DEFAULT NULL COMMENT '清收时间',
这时插入时间通常会是我们常见到的:2020-08-29 12:52:16格式。但是如果datetime(n)中的n不为0呢?
`work_time` datetime(2) DEFAULT NULL COMMENT '清收时间',# datetime(n)中的n最大值为6`work_time` datetime(6) DEFAULT NULL COMMENT '清收时间',
这时在MySQL中会分别显示如下:
2020-08-29 12:52:16.01
2020-08-29 12:52:16.014057
会发现最后有一个小数点且小数点后面会分别对应相应位数的数字–这称之为纳秒。
总结如下:
date
: 只有日期,没有时间;
datetime
:有时间,有日期;
time
:只有时间 ,精确到分秒 ;
timestamp
:时间戳,精确到分秒;
year
:年,如2002,如果写为 2002-01-15,将会进行计算,插入结果为1986
可以使用date_format( )函数进行时间的转换。
SELECT DATE_FORMAT(SYSDATE(),'%Y-%m-%d %H:%i:%s') from dual;
%a | 缩写星期名 |
%b | 缩写月名 |
%c | 月,数值 |
%D | 带有英文前缀的月中的天 |
%d | 月的天,数值(00-31) |
%e | 月的天,数值(0-31) |
%f | 微秒 |
%H | 小时 (00-23) |
%h | 小时 (01-12) |
%I | 小时 (01-12) |
%i | 分钟,数值(00-59) |
%j | 年的天 (001-366) |
%k | 小时 (0-23) |
%l | 小时 (1-12) |
%M | 月名 |
%m | 月,数值(00-12) |
%p | AM 或 PM |
%r | 时间,12-小时(hh:mm:ss AM 或 PM) |
%S | 秒(00-59) |
%s | 秒(00-59) |
%T | 时间, 24-小时 (hh:mm:ss) |
%U | 周 (00-53) 星期日是一周的第一天 |
%u | 周 (00-53) 星期一是一周的第一天 |
%V | 周 (01-53) 星期日是一周的第一天,与 %X 使用 |
%v | 周 (01-53) 星期一是一周的第一天,与 %x 使用 |
%W | 星期名 |
%w | 周的天 (0=星期日, 6=星期六) |
%X | 年,其中的星期日是周的第一天,4 位,与 %V 使用 |
%x | 年,其中的星期一是周的第一天,4 位,与 %v 使用 |
%Y | 年,4 位 |
%y | 年,2 位 |
字符串转换为date:
str_to_date('2016-12-15 16:48:40','%Y-%m-%d %H:%i:%S')
以上为个人经验,希望能给大家一个参考,也希望大家多多支持软件开发网。
边栏推荐
- 原来这才是 BGP 协议
- Actual combat simulation │ JWT login authentication
- Detailed explanation of Audi EDI invoice message
- kotlin 继承
- Informatics Olympiad 1336: [example 3-1] find roots and children
- [QNX hypervisor 2.2 user manual]6.3.1 factory page and control page
- 黑马程序员-软件测试--07阶段2-linux和数据库-09-24-linux命令学习步骤,通配符,绝对路径,相对路径,文件和目录常用命令,文件内容相关操作,查看日志文件,ping命令使用,
- Abc229 summary (connected component count of the longest continuous character graph in the interval)
- Kotlin inheritance
- 更强的 JsonPath 兼容性及性能测试之2022版(Snack3,Fastjson2,jayway.jsonpath)
猜你喜欢
Crystal optoelectronics: ar-hud products of Chang'an dark blue sl03 are supplied by the company
Swagger suddenly went crazy
Pointnext: review pointnet through improved model training and scaling strategies++
Creation of JVM family objects
Chrome开发工具:VMxxx文件是什么鬼
On communication bus arbitration mechanism and network flow control from the perspective of real-time application
华为nova 10系列支持应用安全检测功能 筑牢手机安全防火墙
Key rendering paths for performance optimization
Employment prospects of neural network Internet of things application technology [welcome to add]
Neural network IOT platform construction (IOT platform construction practical tutorial)
随机推荐
Kotlin cycle control
C server log module
需求开发思考
HDU 1097 A hard puzzle
BCG 使用之CBCGPProgressDlgCtrl进度条使用
Niuke Xiaobai month race 7 who is the divine Archer
HDU 1372 & POJ 2243 Knight moves (breadth first search)
更强的 JsonPath 兼容性及性能测试之2022版(Snack3,Fastjson2,jayway.jsonpath)
Wireshark network packet capture
Chrome development tool: what the hell is vmxxx file
Write it down once Net analysis of thread burst height of an industrial control data acquisition platform
[problem] Druid reports exception SQL injection violation, part always true condition not allow solution
node_ Exporter deployment
1009 product of polynomials (25 points) (PAT class a)
Explicit random number
TCP waves twice, have you seen it? What about four handshakes?
Swagger突然发癫
Key rendering paths for performance optimization
kotlin 条件控制
黑马程序员-软件测试--07阶段2-linux和数据库-09-24-linux命令学习步骤,通配符,绝对路径,相对路径,文件和目录常用命令,文件内容相关操作,查看日志文件,ping命令使用,