当前位置:网站首页>Date and time function of MySQL function summary
Date and time function of MySQL function summary
2022-07-25 13:07:00 【51CTO】
Date and time functions are mainly used to process date and time values , The general date function uses
date Type , You can also use datetime perhaps timestamp Parameters of type , But the time part of these values is ignored .

Function to get the current date
curdate() and current_date() The function does the same thing , Set the current date according to yyyy-mm-dd or yyyymmdd The value of the format returns .
See from above , The functions of the two functions are the same , Both return the same current system date .
curdate()+0 Converts the current date to numeric .
Function to get the current time
curtime() and current_time() The function does the same thing , Change the current time to hh:mm:ss perhaps hhmmss The format returns .
See from above , The functions of the two functions are the same , They all go back on the current time of the same system .
Function to get the current date and time
current_timestamp()、localtime()、now() and sysdate() this 4 The functions have the same function , Are values that return the current date and time .
See from above ,4 The results returned by the two functions are the same .
UNIX Time stamp function
UNIX_TIMESTAMP(date) If there is no parameter call , Returns a UNIX Time stamp (‘1970-01-01 00:00:00’GMT Seconds after that ) As an unsigned integer .
among ,GMT(Green wich mean time) Greenwich mean time . If use date To call UNIX_TIMESTAMP(), It will change the parameter value to 1970-01-0100:00:00GMT The number of seconds after the return .
date It could be a DATE character string 、DATETIME character string 、TIMESTAMP Or a local time YYMMDD or YYYYMMDD Number in format .
from_unixtime(date) Function unix Time stamp is converted to normal format , And unix_timestamp(date) Functions are inverse functions to each other . Example :
return UTC Function of date
utc_date() Function returns the current utc( World standard time ) Date value , The format for yyyy-mm-dd perhaps yyyymmdd;
return UTC A function of time
utc_time() Returns the current utc Time value , The format is hh:mm:ss perhaps hhmmss;
Function to get month MONTH(date) and MONTHNAME(date)
month(date) The function returns date The corresponding month .
monthname(date) Function returns the date date The full English name of the corresponding month ;
Get the function of week DAYNAME(d)、DAYOFWEEK(d) and WEEKDAY(d)
dayname(d) The function returns d English name of the corresponding working day ;
dayofweek(d) The function returns d Index of the corresponding week ( Location ,1 Means Sunday ,2 It means Monday , And so on ,7 It means Saturday );
weekday(d) return d Corresponding weekday index :0 It means Monday ,1 Tuesday , And so on ,6 Means Sunday .
Get the function of week WEEK(d) and WEEKOFYEAR(d)
week(d) Calculate the date d It's the week of the year .
week() The two parameter form of allows you to specify whether the week begins on Sunday or Monday , And whether the return value range is 0~53 perhaps 1~53. if mode Parameters are omitted , Then use default_week_format The value of the system argument , Here's the picture :

for example : Use week() The function queries which week of the year the specified date is ;
weekofyear(d) Calculate the week of the year in which a day is located , The scope is 1~53, amount to week(d,3);
for example : Use weekofyear(d) The query specifies the week of the year ;
As can be seen from the above , The two functions return the same result .
Function to get the number of days DAYOFYEAR(d) and DAYOFMONTH(d)
dayofyear(d) The function returns d It's the day of the year , The scope is 1~366;
for example : Use dayofyear() Function returns the position of the specified date in a year ;
dayofmonth(d) The function returns d It's the day of the month , The scope is 1~31;
for example : Use dayofmonth() Function returns the position of the specified date in a month , There must be a specific year .
Get year 、 quarter 、 Hours 、 As a function of minutes and seconds
year(date) return date Corresponding year , The scope is 1970~2069;
tip :
00~69 Convert to 2000~2069,70~99 Convert to 1970~1999.
quarter(date) return date The corresponding quarterly value of the year , The scope is 1~4;
minute(time) return time The corresponding number of minutes , The scope is 0~59.
second(time) return time The corresponding number of seconds , The scope is 0~59.
Function to get the specified value of the date EXTRACT(type from date)
extract(type from date) The time interval type specifier used by the function is the same as date_add() or date_sub() identical , But it takes a part from the date , Instead of performing date operations .
type For different values , The values returned are different :
-
year: Only return year value -
year_month: Return year and month -
day_minute: Return day 、 Hours and minutes
Time and second conversion function
time_to_sec(time) Returns the number of seconds converted time Parameters .
Change the formula to : Hours *3600+ minute *60+ second
sec_to_time(seconds) Return is converted into hours 、 Minutes and seconds seconds Parameter values , The format for hh:mm:ss perhaps hhmmss.
You can see from above ,time_to_sec and sec_to_time They're inverse functions to each other .
A function that calculates the date and time
The functions for calculating date and time are :
- date_add()
- adddate()
- date_sub()
- subdate()
- addtime()
- subtime()
- date_diff()
stay date_add(date, interval expr type) and date_sub(date, interval expr type) in :
-
date It's a datetime perhaps date value , Used to specify the starting time . -
expr It's an expression , Used to specify the interval value added or subtracted from the start date . For time intervals with negative values ,expr You can use a symbol - start . -
type For the keyword , Indicates how the expression is interpreted . 
if date The parameter is one date value , The calculation will only include year、month and day part ( There is no time part ), The result is a date value , otherwise , The result will be a datetime value .
date_add(date, interval expr type) and adddate(date, intervar expr type) Two functions have the same function , Perform the addition of dates .
date_sub(date, interval expr type) perhaps subdate(date, interval expr type) Two functions have the same function , Perform the subtraction of the date .
addtime(date, expr) Function will expr Value added to date, And return the modified value ,date Value a date or datetime expression , and expr It's a time expression .
subtime(date,expr) Function will date subtract expr value , And return the modified value .
among ,date Is a date or date time expression , and expr It's a time expression .
You can see from above , Only the time is calculated , The date is not counted .
Use datediff() Function to calculate the number of days between two dates ;
Functions that format dates and times
date_format(date,format) according to format Display... In the specified format date value .
The main format The format is as follows :

Example : Use date_format() Function to format the output date and time values ;
time_format(time, format) According to the expression format The request shows the time time .
expression format Specifies the format of the display . because time_format(time, format) Only processing time , therefore format Only use time format .
Example : Use time_format() Function format input time value .
get_format(val_typr, format_type) Return the display format of date time string :
-
val_type Represents the date data type , Include date、datetime and time; -
format_type Indicates the format display type , Include eur、interval、iso、jis、usa.
get_format The display format of the returned string based on the combination of two value types is as follows :

Example : Use get_format() Function to display format strings under different format types .
thus , In this paper, the end .
For more information, go to VX official account “ Operation and maintenance home ” , Get the latest article .
------ “ Operation and maintenance home ” ------
------ “ Operation and maintenance home ” ------
------ “ Operation and maintenance home ” ------
linux Interrupt exception ,linuxuml modeling ,linux Compile multiple source files ,linux How to update the time ,linux Paging video tutorial in ,
linux How to decompress gzip,linux Unzip a directory , How to close linux gateway ,linux Document structure diagram ,linux Improve the permission to execute ,
linux What mark is used in the document , Network security linux strengthening ,linux Check email in ,linux Change the background color ,linux Build a website under the environment .
边栏推荐
- MySQL remote connection permission error 1045 problem
- How to understand metrics in keras
- 业务可视化-让你的流程图'Run'起来(3.分支选择&跨语言分布式运行节点)
- How to use causal inference and experiments to drive user growth| July 28 tf67
- 零基础学习CANoe Panel(14)——二极管( LED Control )和液晶屏(LCD Control)
- Force deduction 83 biweekly T4 6131. The shortest dice sequence impossible to get, 303 weeks T4 6127. The number of high-quality pairs
- Leetcode 0133. clone diagram
- What does the software testing process include? What are the test methods?
- Deep learning MEMC framing paper list
- AtCoder Beginner Contest 261E // 按位思考 + dp
猜你喜欢

如何理解Keras中的指标Metrics

The programmer's father made his own AI breast feeding detector to predict that the baby is hungry and not let the crying affect his wife's sleep

A hard journey
![SSTI template injection vulnerability summary [bjdctf2020]cookie is so stable](/img/19/0b943019fe1c959c4b79035a814410.png)
SSTI template injection vulnerability summary [bjdctf2020]cookie is so stable

Make a general cascade dictionary selection control based on jeecg -dictcascadeuniversal

Selenium use -- installation and testing

Eccv2022 | transclassp class level grab posture migration

程序的内存布局

OAuth,JWT ,OIDC你们搞得我好乱啊

零基础学习CANoe Panel(15)—— 文本输出(CAPL Output View )
随机推荐
[机器学习] 实验笔记 – 表情识别(emotion recognition)
What is ci/cd?
[300 opencv routines] 239. accurate positioning of Harris corner detection (cornersubpix)
Leetcode 1184. distance between bus stops
牛客论坛项目部署总结
B tree and b+ tree
全球都热炸了,谷歌服务器已经崩掉了
ECCV2022 | TransGrasp类级别抓取姿态迁移
我想问DMS有没有定时备份某一个数据库的功能?
[problem solving] ibatis.binding BindingException: Type interface xxDao is not known to the MapperRegistry.
吕蒙正《破窑赋》
Introduction to web security UDP testing and defense
conda常用命令:安装,更新,创建,激活,关闭,查看,卸载,删除,清理,重命名,换源,问题
Common operations for Yum and VIM
艰辛的旅程
零基础学习CANoe Panel(12)—— 进度条(Progress Bar)
Clickhouse notes 03-- grafana accesses Clickhouse
485 communication (detailed explanation)
shell基础知识(退出控制、输入输出等)
Seven lines of code made station B crash for three hours, but "a scheming 0"