当前位置:网站首页>interviewter:介绍一下MySQL日期函数
interviewter:介绍一下MySQL日期函数
2022-04-23 05:42:00 【ShuangTwo】
interviewter: 怎样获取当前日期和时间
select now();
select sysdate();
select current_timestamp;
select current_timestamp();
select localtime;
select localtime();
select localtimestamp;
select localtimestamp();
interviewter: 如果我只想打印日期,应该怎么处理
select curdate();
select current_date;
select current_date();
select utc_date;
select utc_date();
interviewter: 现在我不想查看日期了,我想看一下具体时间是多少
select curtime();
select current_time;
select current_time();
select utc_time;
select utc_time();
interviewter: 日期与时间戳的转换了解吗
select from_unixtime(1649402591); -- 将UNIX时间戳的时间转换为普通格式的时间
2022-04-08 15:23:11
select unix_timestamp("2022-04-08 15:23:11"); -- 将时间date以UNIX时间戳的形式返回
1649402591
-- 这两个方法用于时间戳和日期的转换
select unix_timestamp(); -- 以UNIX时间戳的形式返回当前时间
1649402591
-- 简记unix_timestamp(),有时间参数就转换为为对应时间戳,无参数就获取当前时间戳,返回结果都是时间戳
interviewter: 如果我只想获取日期时间中某一部分怎么做呢
select now(); -- 2022-04-08 15:29:55
select year("2022-04-08 15:29:55");
select month("2022-04-08 15:29:55");
select day("2022-04-08 15:29:55");
select hour("2022-04-08 15:29:55");
select minute("2022-04-08 15:29:55");
select second("2022-04-08 15:29:55");
select dayname("2022-04-08 15:29:55"); --返回星期几的英文名称
select monthname("2022-04-08 15:29:55"); --返回月份的英文名称
select weekday("2022-04-08 15:29:55"); --周1是0,周2是1......周日是6
select quarter("2022-04-08 15:29:55"); --返回日期对应的季度
-- 返回一年中的第几周
select week("2022-04-08 15:29:55");
select weekofyear("2022-04-08 15:29:55");
select dayofyear("2022-04-08 15:29:55"); -- 返回日期是一年中的第几天
select dayofmonth("2022-04-08 15:29:55"); -- 返回日期是所在月份的第几天
select dayofweek("2022-04-08 15:29:55"); -- 返回周几,周日是1,周一是2......周六是7
-- 2022-04-08 15:29:55
select extract(year from "2022-04-08 15:29:55"); -- 2022
select extract(month from "2022-04-08 15:29:55"); -- 4
select extract(week from "2022-04-08 15:29:55"); -- 14
select extract(day from "2022-04-08 15:29:55"); -- 8
select extract(hour from "2022-04-08 15:29:55"); -- 15
select extract(minute from "2022-04-08 15:29:55"); -- 29
select extract(second from "2022-04-08 15:29:55"); -- 55
select extract(year_month from "2022-04-08 15:29:55"); -- 202204
select extract(day_hour from "2022-04-08 15:29:55"); -- 815
select extract(day_minute from "2022-04-08 15:29:55"); -- 81529
select extract(day_second from "2022-04-08 15:29:55"); -- 8152955
select extract(hour_minute from "2022-04-08 15:29:55"); -- 1529
select extract(hour_second from "2022-04-08 15:29:55"); -- 152955
select extract(minute_second from "2022-04-08 15:29:55"); -- 2955
interviewter: 我想知道某一时间有多少秒怎么转换,反过来呢
select time_to_sec("15:29:55")
select sec_to_time(55795);
interviewter: 日期时间间隔方法会使用吗
-- 返回与给定日期时间相差INTERVAL时间段的日期时间(向上)
select date_add("2022-04-08 15:29:55", interval 1 year);
select date_add("2022-04-08 15:29:55", interval 1 month);
select date_add("2022-04-08 15:29:55", interval 1 day);
select date_add("2022-04-08 15:29:55", interval 1 hour);
select date_add("2022-04-08 15:29:55", interval 1 minute);
select date_add("2022-04-08 15:29:55", interval 1 second);
select adddate("2022-04-08 15:29:55", interval 2 year) -- 该方法和date_add一样
-- 返回与给定日期时间相差INTERVAL时间间隔的日期时间(向下)
select date_sub("2022-04-08 15:29:55", interval 1 year);
select date_sub("2022-04-08 15:29:55", interval 1 month);
select date_sub("2022-04-08 15:29:55", interval 1 day);
select date_sub("2022-04-08 15:29:55", interval 1 hour);
select date_sub("2022-04-08 15:29:55", interval 1 minute);
select date_sub("2022-04-08 15:29:55", interval 1 second);
select subdate("2022-04-08 15:29:55", interval 2 year) -- 该方法和date_sub一样
-- 简记为:add加,sub减,这两类方法的主要区别在于返回向上的时间间隔和向下的时间间隔
select addtime("2022-04-08 15:29:55", 10); --返回给定时间加上多少秒后的时间,将第二个参数为秒
select subtime("15:29:55", "15:20:00"); -- 返回time1减去time2后的时间
select datediff("2022-04-08", "2022-04-01"); -- 返回date1减去date2后的日期
select timediff("15:29:55", "15:20:00"); -- 返回time1减去time2的时间间隔
select last_day("2022-04-08 15:29:55"); -- 返回给定日期时间所在月的最后一天的日期
select adddate("2022-04-08 15:29:55", 10); --返回给定日期加上多少天后的时间,将第二个参数
interviewter:日期和字符串这怎么互相转换
select date_format("20220408152955", '%Y-%m-%d'); -- 将字符串转换为日期
select time_format("20220408152955", '%H:%i:%s') -- 将字符串转换为时间
select str_to_date("2022-04-08 15:29:55", '%Y-%m-%d %H:%i:%s') -- 将字符串转换为日期时间
| 格式符 | 说明 | 格式符 | 说明 |
|---|---|---|---|
| %Y | 4位数字表示年份 | %y | 表示两位数字表示年份 |
| %M | 月名表示月份(January,…) | %m | 两位数字表示月份(01,02,03。。。) |
| %b | 缩写的月名(Jan.,Feb.,…) | %c | 数字表示月份(1,2,3,…) |
| %D | 英文后缀表示月中的天数(1st,2nd,3rd,…) | %d | 两位数字表示月中的天数(01,02…) |
| %e | 数字形式表示月中的天数(1,2,3,4,5…) | ||
| %H | 两位数字表示小数,24小时制(01,02…) | %h和%I | 两位数字表示小时,12小时制(01,02…) |
| %k | 数字形式的小时,24小时制(1,2,3) | %l | 数字形式表示小时,12小时制(1,2,3,4…) |
| %i | 两位数字表示分钟(00,01,02) | %S和%s | 两位数字表示秒(00,01,02…) |
| %W | 一周中的星期名称(Sunday…) | %a | 一周中的星期缩写(Sun.,Mon.,Tues.,…) |
| %w | 以数字表示周中的天数(0=Sunday,1=Monday…) | ||
| %j | 以3位数字表示年中的天数(001,002…) | %U | 以数字表示年中的第几周,(1,2,3。。)其中Sunday为周中第一天 |
| %u | 以数字表示年中的第几周,(1,2,3。。)其中Monday为周中第一天 | ||
| %T | 24小时制 | %r | 12小时制 |
| %p | AM或PM | %% | 表示% |
版权声明
本文为[ShuangTwo]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_43356538/article/details/124046008
边栏推荐
猜你喜欢

基于ssm 包包商城系统

MySQL realizes master-slave replication / master-slave synchronization

多线程与高并发(1)——线程的基本知识(实现,常用方法,状态)

Fletter next generation graphics renderer impaller

线程的底部实现原理—静态代理模式

Interview Basics

SQL statement simple optimization

Flutter 新一代圖形渲染器 Impeller

Data mining -- understanding data

JVM系列(4)——内存溢出(OOM)
随机推荐
mysql sql优化之Explain
Sword finger offer II 022 The entry node of the link in the linked list
Range of numbers (dichotomous classic template topic)
Idea plug-in --- playing songs in the background
框架解析2.源码-登录认证
opensips(1)——安装opensips详细流程
踩坑:nacos利用startup.cmd -m standalone启动错误
MySQL realizes master-slave replication / master-slave synchronization
‘EddiesObservations‘ object has no attribute ‘filled‘
尚硅谷 p290 多态性练习
Common interview questions - 4 (MySQL)
一文读懂当前常用的加密技术体系(对称、非对称、信息摘要、数字签名、数字证书、公钥体系)
Flutter 新一代圖形渲染器 Impeller
Issue 36 summary of atcoder beginer contest 248
Pilotage growth · ingenuity empowerment -- yonmaster developer training and pilotage plan is fully launched
DBCP使用
Common protocols of OSI layer
POI exports to excel, and the same row of data is automatically merged into cells
TypeScript interface & type 粗略理解
freemark中插入图片