MySQL comes with many built-in functions that allow you to manipulate data. These functions are grouped into categories โ date functions, string functions, mathematic functions, and others.
Date functions give you numerous options on how to modify, calculate, and convert date, time, and datetime expressions in MySQL.
In this tutorial, you will learn about MySQL date and time functions and how they work, on practical examples.

Note: For more MySQL functions and commands, check out our MySQL Cheat Sheet.
Date-Related Functions
CURDATE OR CURRENT_DATE
Return the current date in the โYYY-MM-DDโ or โYYYYMMDDโ format with the CURDATE
OR CURRENT_DATE
command.
The basic syntax:
For example, if you run:
MySQL responds with the current date in the format:
DATE
Return the date from a datetime expression using the DATE
command.
The basic syntax:
For instance, if you run:
The output is:
DATE_ADD or ADDDATE
Add a time/date value to a date expression with the DATE_ADD
or ADDDATE
function.
The basic syntax:
Replace date with the date expression you want to add a time/date to. The value unit is the time/date you want to add. It needs to be expressed in a numeric value along with the unit of the value.
The unit can be:
- SECOND
- MINUTE
- HOUR
- DAY
- WEEK
- MONTH
- QUARTER
- YEAR
- SECOND_MICROSECOND
- MINUTE_MICROSECOND
- MINUTE_SECOND
- HOUR_MICROSECOND
- HOUR_SECOND
- HOUR_MINUTE
- DAY_MICROSECOND
- DAY_SECOND
- DAY_MINUTE
- DAY_HOUR
- YEAR_MONTH
For example, if you run:
The output returns the result:
DATE_FORMAT
Format a date using DATE_FORMAT
.
The basic syntax:
The date is the date expression you want to reformat, while the format is a combination of the following specifiers:
For instance, if you run:
The output displays the result:
DATE_SUB or SUBDATE
Subtract a time/date value to a date expression with the DATE_SUB
or SUBDATE
function.
The basic syntax:
Replace date with the date expression you want to subtract from. The value unit is the time/date you want to subtract. It needs to be expressed in numeric value along with the unit of the value.
Find a list of unit types in the DATE_ADD section.
For instance, if you run:
The output returns the result:
DATEDIFF
Return the number of days between two date expressions with the DATEDIFF
function.
The basic syntax:
For example:
Returns the result:
EXTRACT
To extract part of a date/datetime expression, use the EXTRACT
function.
The basic syntax:
In the command, you need to specify which unit you want to extract from the specified date.
Find a list of units you can use in the DATE_ADD description.
For instance, when you run:
You get the result:
GET_FORMAT
Return a format string (a combination of specifiers) as specified in the argument with GET_FORMAT
. This function is often used with DATE_FORMAT
.
The basic syntax:
Use this function with date, time, and datetime expressions.
The format can be:
- โEURโ
- โUSAโ
- โJISโ
- โISOโ
- โINTERNALโ
There is a finite number of results you can get using the GET_FORMAT
function. Below you will find a list of all the function calls and their results.
For instance, you could combine the function with DATE_FORMAT
, as in the following example:
Where the result is:
MAKEDATE
Return a date expression from a specified year and day of year using the MAKEDATE
function.
The basic syntax:
For example, if you run:
The output shows the result:
STR_TO_DATE
Format a date from a string with STR_TO_DATE
and return a date/datetime value.
The basic syntax:
The string is what you want to reformat, while the format is a combination of specifiers that describe each element of the string.
You can find a list of specifiers and their meaning in the DATE_FORMAT section.
For instance, if you run:
The output shows:
SYSDATE
To return the current date and time in the format โYYYY-MM-DD hh:mm:ssโ or โYYYYMMDDHHMMSS.uuuuuuโ, use the SYSDATE
function.
The basic syntax:
You can add the fsp
argument to include fractional second precision (0-6). In that case, the syntax is SYSDATE(fsp);
.
The command shown below:
At this time gives the result:
UTC_DATE
Return the current Coordinated Universal Time (UTC) date value in the โYYYY-MM-DDโ or โYYYYMMDDโ format with the UTC_DATE
function.
The basic syntax:
For example, running the following command:
Returns the current date which is:
Time-Related Functions
ADDTIME
Add a time interval to a specified time/datetime expression using ADDTIME
.
The basic syntax:
For instance, if you run:
You get the result:
CONVERT_TZ
Convert a time/datetime expression from one time zone to another using the ADDTIME
function.
The basic syntax:
For example, when you run:
MySQL converts the specified datetime to +10:00 time zone:
CURTIME or CURRENT_TIME
Return the current time using the CURTIME
or CURRENT_TIME
function. The result returns the time in the โhh:mm:ssโ or โhhmmssโ format.
The basic syntax:
You can include fractional second precision (from 0 to 6) by adding the fsp
argument.
For example, the following command shows the current time with three fractional second precision:
The output responds with the result:
HOUR
Return the hour of the specified time/datetime with the HOUR
function.
The basic syntax:
For instance, if you run:
The result is:
MAKETIME
Return a time expression from the specified hour, minute, and second values using the MAKETIME
function.
The basic syntax:
For example, you can run:
Where the output displays:
MICROSECOND
Return the microseconds of the specified time/datetime expression with MICROSECOND
.
The basic syntax:
For example, you can run:
Where the result is:
MINUTE
Return the minutes of the specified time/datetime expression using the MINUTE
function.
The basic syntax:
For instance, if you run the command:
The result is:
SEC_TO_TIME
Return a time value from a specified seconds value with the SEC_TO_TIME
function.
The basic syntax:
For instance, if you run the command:
The output is:
SUBTIME
Subtract a time value from a time/datetime expression using the SUBTIME
function.
The basic syntax:
For example, when you run:
The output is:
TIME
To return the time value from a datetime expression, use the TIME
function.
The basic syntax:
For instance:
Gives the result:
TIME_FORMAT
Format a time value into the specified format with TIME_FORMAT
.
The basic syntax:
The format is a combination of specifiers. You can find a list of all specifiers and their meaning in the description of the DATE_FORMAT function.
For example, by running:
You get the output:
TIME_TO_SEC
To return the time value converted into seconds use the TIME_TO_SEC
.
The basic syntax:
For instance, when you run:
The result is:
TIMEDIFF
Calculate the difference between two time/datetime expressions with the TIMEDIFF
function. In this case, the result is always in time value.
The basic syntax:
For instance, when you run:
The output returns:
TO_SECONDS
To convert a date/datetime expression into seconds, use the function TO_SECONDS
. The result is the number of seconds between 0 and the specified date/datetime.
The basic syntax:
For example, if you run the command:
The result is:
UTC_TIME
Return the current UTC time value with UTC_TIME
. It returns the time value in the โHH:MM:SSโ or โHHMMSSโ format.
The basic syntax:
For example, if you run:
You get the result at this point of time is:
Note: Do you know how a traditional relational database compares to a document-oriented database management system? Learn more in this analysis of MySQL Vs. MongoDB.
Timestamp-Related Functions
CURRENT_TIMESTAMP or LOCALTIMESTAMP
To return the current date and time, use CURRENT_TIMESTAMP
or LOCALTIMESTAMP
. The result returns in the โYYYY-MM-DD HH-MM-SSโ or โYYYYMMDDHHMMSS.uuuuuuโ format.
The basic syntax:
For example, by running:
The current result is:
FROM_UNIXTIME
Return a date/datetime expression from a timestamp in the Unix format with FROM_UNIXTIME
.
The basic syntax:
If you run the command without an argument specifying the format, it returns the result in the โYYYY-MM-DD hh:mm:ssโ or โYYYYMMDDhhmmssโ format.
For instance, if you run:
You get the result:
TIMESTAMP
To return a datetime expression from a date or datetime value, use the function TIMESTAMP
. If you add two arguments, the output returns the sum of the arguments.
The basic syntax:
For example, when you run the command:
The output shows the result:
TIMESTAMPADD
Add a time value to a date/datetime expression using the TIMESTAMPADD
function.
The basic syntax:
The unit can be:
- FRAC_SECOND
- SECOND
- MINUTE
- HOUR
- DAY
- WEEK
- MONTH
- QUARTER
- YEAR
For instance, the following command adds 3 days to the specified date:
Therefore, the output shows:
TIMESTAMPDIFF
To calculate the difference between two date/datetime expressions, use TIMESTAMPDIFF
. The function subtracts one datetime value from the other in the specified unit.
The basic syntax:
You can find a list with different types of units, check out the list in the section above.
For example, you can calculate the difference between the following two dates in days:
Where the result is:
UNIX_TIMESTAMP
Return a Unix timestamp from a date/datetime expression with the UNIX_TIMESTAMP
function. The Unix timestamp represents seconds between the specified datetime and โ1970-01-01 00:00:00โ UTC.
The basic syntax:
For instance, running the following command:
Gives the result:
UTC_TIMESTAMP
Return the current UTC date and time value with UTC_TIMESTAMP
. It returns the datetime value in the โYYYY-MM-DD HH:MM:SSโ or โYYYYMMDDHHMMSS.uuuuuuโ format.
The basic syntax:
For example, the command:
Returns output in the same format as the one below:
Day/Week/Month/Year-Related Functions
DAY
Return the day of a month from a specified date/datetime expression with the DAY
function.
The basic syntax:
If you run the command below:
The output returns the result:
DAYNAME
Return the name of the weekday from a specified date/datetime expression using the DAYNAME
function.
The basic syntax:
For example, when you run the command:
MySQL responds with the result:
DAYOFMONTH
Return the day of a month from a specified date/datetime expression with DAYOFMONTH
.
The basic syntax:
For example, when you run the command:
MySQL responds with the result:
DAYOFWEEK
Return the day of week in numerical value from the specified date/datetime expression using DAYOFWEEK
.
The basic syntax:
Running the command below:
Gives the response:
DAYOFYEAR
Return the day in a year from the specified date/datetime expression using the function DAYOFYEAR
.
The basic syntax:
For instance, when you run the command:
The output gives the result:
FROM_DAYS
Return a date expression from a numeric representation of a day using the function FROM_DAYS
.
The basic syntax:
For example, running:
The MySQL prompt responds with the result:
LAST_DAY
Return the last day of the month from a specified date/datetime with the LAST_DAY
function.
The basic syntax:
For instance, if you run the following command:
The output responds with:
MONTH
Return the month (in numeric value) from a specified date/datetime by using the MONTH
function.
The basic syntax:
For example, when you run:
MySQL responds with:
MONTHNAME
Return the name of the month from a specified date/datetime with the MONTHNAME
function.
The basic syntax:
If you run the command:
You get the following response:
PERIOD_ADD
To add a specified number of months to a period, use the PERIOD_ADD
function.
The basic syntax:
The period is defined in the format YYMM or YYYYMM, while the number is the number of months you want to add.
For example:
Gives the result:
PERIOD_DIFF
Return the number of months between two periods with PERIOD_DIFF
.
The basic syntax:
Each period should be in the format YYMM or YYYYMM.
For instance:
Produces the result:
QUARTER
To return a quarter of a year from a specified date/datetime, use the function QUARTER
.
The basic syntax:
For example, by running the command:
The output responds with:
TO_DAYS
Convert a date/datetime expression to a numeric representation of a day with TO_DAYS
.
The basic syntax:
For instance, for the command:
The result is :
WEEK
To return the week number from a specified date, use the function WEEK
.
The basic syntax:
You can also include the mode argument, in which case the syntax is WEEK(date,mode);
.
The mode argument specifies from which day the week starts. If there is no argument, it uses 0
mode by default.
The following table describes each mode:
For example, if you run:
The output responds with:
WEEKDAY
Return the weekday from the specified date in numeric value with WEEKDAY
. Each number represents one of the weekdays โ Monday is 0
, Tuesday is 1
, and so on.
The basic syntax:
For example, running the following command:
Gives the response:
WEEKOFYEAR
To return the number of a week in a year, use the WEEKDAY
function.
The basic syntax:
For instance, when you run the command:
The output shows the result:
YEAR
Return the year from the specified date with the YEAR
function.
The basic syntax:
If you run the following command:
You get the result:
YEARWEEK
Return the year and week number from the specified date using the YEARWEEK
function.
The basic syntax:
You can add a mode
argument to the basic syntax to specify the day from which the week begins. To see a list of modes and their meaning, check out the table in the WEEK
function.
For example, when you run the command:
The output displays:
Conclusion
This article should help you find all the MySQL date (and time) functions. With descriptions for each function and practical examples, you should be able to apply them with ease.
To have a better understanding of different data types, we suggest to check out our article on MySQL Data Types.