当前位置:网站首页>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
边栏推荐
猜你喜欢

OSI层常用协议

MySQL lock mechanism

mysql sql优化之Explain

Flutter nouvelle génération de rendu graphique Impeller

一文读懂当前常用的加密技术体系(对称、非对称、信息摘要、数字签名、数字证书、公钥体系)

类的加载与ClassLoader的理解

2-软件设计原则

引航成长·匠心赋能——YonMaster开发者培训领航计划全面开启

Isosceles triangle - the 9th Lanqiao provincial competition - group C

Breadth first search topics (BFS)
随机推荐
MDN文档里面入参写法中括号‘[]‘的作用
World and personal development
Jiugong magic square - the 8th Lanqiao provincial competition - group C (DFS and comparison of all magic square types)
转置卷积(Transposed Convolution)
软件架构设计——软件架构风格
JDBC连接数据库
Meta annotation (annotation of annotation)
shell指令学习1
‘EddiesObservations‘ object has no attribute ‘filled‘
No.1.#_ 6 Navicat shortcuts
QT compressed folder
io. lettuce. core. RedisCommandExecutionException: ERR wrong number of arguments for ‘auth‘ command
jdbc入门\获取数据库连接\使用PreparedStatement
Shell instruction learning 1
Date增加天数
Pavlov and hobbies
JS number capitalization method
Isosceles triangle - the 9th Lanqiao provincial competition - group C
Flutter 新一代图形渲染器 Impeller
提升独立站转化率攻略 | 挽回弃购用户