MySQL Date and Time function
The date function displays day, year, month, time, and current date. It shows the date and time as per the requirement of the applications. The data either store on the table or display directly on an output page. Type of the MySQL date and time functions show below. You can see the date function, time function, and date and time function together.
Syntax
MySQL function uses the following syntax to display date and time.
SELECT function_name(date/time);
MySQL date and time function require multiple values or arguments.
SELECT function_name(date_time1, date_time2);
MySQL date and time function syntax with additional values are shown below.
SELECT function_name(date/time) + value;
Date and Time functions
List of the date and time function with description shows below.
Function | Description |
ADD_DATE | The adddate() function shows a date interval as per requirement. |
ADD_TIME | The add_time() function displays a time interval as per requirement. |
CURDATE | The curdate() function displays a current date interval. |
CURRENT_DATE | The current_date() function shows a current date interval. |
CURRENT_TIMESTAMP | The current_timestamp() function is a type of the MySQL date and time function. This function displays the current date and time. |
CURTIME | The curtime() function shows a current time. |
DATE | The date() function extracts the date interval as per requirement. This function does not display time as output. |
DATEDIFF | The datediff() function displays the days between two dates. |
DATE_ADD | The date_add() function adds a date to date and shows the date. |
DATE_SUB | The date_sub() function subtracts a date to date and returns the date. |
DAY | The day() function displays the day of the date or month. |
DAYOFMONTH | The dayofmonth() shows the month of the date. |
DAY OF WEEK | The dayofweek() function is a type of the MySQL date and time function. This function returns the week of the date. |
DAYOFYEAR | The dayofyear() function is a type of the MySQL date and time function. This function displays the year of the date. |
FROM_DAYS | The from_days() function returns the numerical date value of the given date. |
HOUR | The hour() function is a type of the MySQL date and time function. This function shows the hours of the date and time. |
LOCAL TIME | The localtime() function is a type of the MySQL time function. This function displays the current date and time. |
LOCALTIMESTAMP | The localtimestamp() function displays the current date and time |
MAKEDATE | The makedate() function creates and displays the date. |
MAKETIME | The maketime() function is a type of the MySQL time function. This function creates and displays the time (hour, minute, and second). |
MICROSECOND | The microsecond() function shows the microsecond of the given date and time. |
MINUTE | The minute() function displays the minute of the given time or date-time. |
MONTH | The month() function shows the month of the given date. |
MONTHNAME | The monthname() function returns the month of the given date. |
NOW | The now() function is a type of the MySQL date and time function. This function shows the current date and time. |
PERIOD_ADD | The period_add() function is type of the MySQL date function. This function inserts months of the date as a period. This function displays year and month but not date. |
PERIOD_DIFF | The period_diff() function displays the difference between the two periods. |
QUARTER | The quarter() function is a type of the MySQL date and time function. This function shows the quarter of the given year. |
SECOND | The second() function returns the second of the given datetime. |
SEC_TO_TIME | The sec_to_time() function displays a time of the datetime into seconds. |
STR_TO_DATE | The str_to_date() function shows a date into a string format. |
SUBDATE | The subdate() function subtracts a date of the datetime and returns the date. |
SUBTIME | The subtime() function subtracts a time of the datetime and returns the time. |
TIME | The time() function is type of MySQL time function. This function shows the time of the given datetime. |
TIME_FORMAT | The time_format() function displays time with the required format. |
TIME_TO_SEC | The time_to_sec() function converts a given time into seconds. |
TIMEDIFF | The timediff() function is a type of the MySQL time function. This function differences between two times or datetime. |
TIMESTAMP | The timestamp() function returns a date or datetime. |
TO_DAYS | The to_days() function shows the number of days between two dates. |
WEEK | The week() function displays the week number of the date. |
WEEKDAY | The weekday() function displays the number of the weekday of the date. |
WEEKOFYEAR | The weekofyear() function shows the weeks of the date. |
YEAR | The year() function returns the year of the date. |
YEAR WEEK | The yearweek() function displays the year and week of the given date. |
Examples of the MySQL Date and Time functions
The following example determines and returns a date, time and both date/time information. There are multiple functions to displays current, past and required dates and times.
The following statements shows you the current date, time with the respective function. You can return date and time simultaneously using the MySQL function. Here, you can use a single function or multiple functions in the query.
Example
The single date and time function's example and output are shown below.
mysql> SELECT CURRENT_TIMESTAMP();
Output
In this output image, the table returns the current date, time in a particular format. The date function displays in the "year-month-day" format. The time displays in the "hour:minute:second" format.
Example
Multiple MySQL functions used to display the date and time output. Here is the statement:
mysql> SELECT now(), CURTIME(), CURDATE();
Output
You can get three columns and one row. These three columns represent three date and time functions. The Now() function return instant date and time as output. The CURTIME() and CURDATE() displays time and date values, respectively.
Examples of specific dates and times with the output
MySQL date/time function displays a specific feature of the date or time. You can return only the day, month, year, and week of the date. Here, you can get the second, hours, and minutes of the time.
Example
Below query shows you date, day, week, month and year. If you input time value in the specific date function then output returns only date and its features. This function does not show you time value.
mysql> select date('2021-08-30, 14:57:40') as date,
day('2021-08-30, 14:57:40') AS day,
week('2021-08-30, 14:57:40') AS week,
month('2021-08-30, 14:57:40') AS month,
year('2021-08-30, 14:57:40') AS year;
Output
Here, you see the specific date information like day, month, year, and week. Here time is not working with any function. Each column specifies a particular date value as per function. This example uses the day, week, date, year, and month function with values.
Example
The following query displays time, second, minute, hours using MySQL date/time function. If you enter date in the function then output returns only date and its function.
mysql> select time('2021-08-30, 14:57:40') as time,
minute('2021-08-30, 14:57:40') AS minute,
hour('2021-08-30, 14:57:40') AS hour,
second('2021-08-30, 14:57:40') AS second;
Output
The above output works on the time function of MySQL. The first column shows the required time in the table. The minute(), hour(), and second() functions used to display specific time part as per functions. You cannot use to display the date using these functions.
Examples of specific date functions with output
MySQL date function works on the date function as per requirement. You can modify, add, and maintain the date information of the table. The following examples show you the operation of the date using some functions.
Example
MySQL date function uses four functions or methods in the system. You can add and subtract date as per interval. The query displays difference between two dates and shows dates with string value.
mysql> select adddate('2021-06-21', interval 13 hour) AS date,
-> datediff('2021-06-26', '2021-06-22') AS differences,
-> from_days(3401232) AS days,
-> subdate('2021-06-21', interval 13 hour) AS dates;
Output
The above table image shows different functions and their value as output. The first column adds date with the 13-hour interval, and the last column subtracts date with the given interval. The second column returns the difference between the two dates, and the third column shows a date of the given number.
Example
MySQL example displays date as per required format. You can displays weekday and month of the date using expressions. You can choose date format with display method using date_format() condition. The date format function provides several expressions with percentage sign. Here, you can change expressions sequence to display date in the table.
mysql> select date_format('2021-06-21', '%a - %b') AS day_month,
date_format('2021-06-21', '%e - %M - %Y') AS date,
date_format('2021-06-21', '%d / %c / %y') AS dates;
Output
The above image shows you three columns and one row of the date value. The first column displays the day of the week and month of the date using "%a" and "%b" expressions. The second and third columns show the date with the given format. You can use four-digit and two-digit year format with the "%Y" AND %y" operators, respectively.
Examples of specific dates and times with
You can use several functions to operate and maintain time information. The date/time function required or current time for their operation. You can display time as per the required format.
Example
The time function uses several functions with time value in the system. You can operate time and maintain it using add and subtract time function. The query displays time information with required interval and operations.
mysql> select addtime('14:57:40', '01:57:40') AS aadtime,
timediff('12:57:40', '02:57:40') AS differences,
time_to_sec('01:57:40') AS second,
sec_to_time('440') AS time,
subtime('14:57:40', '01:57:40') AS subtime;
Output
The above table shows five columns and one row with time information. Each column represents a different date/time function. The first column returns added time, and the last column returns subtracted time of the data. The second column shows the difference between times in time format. The third function converts time into second. The fourth function converts the given second into time.
Example
MySQL time function displays time in different format. You can displays weekday and month of the date using expressions. You can choose date format with display method using date_format() condition. The date format function provides several expressions with percentage sign. Here, you can change expressions sequence to display date in the table.
mysql> select time_format('14:57:40', '%h %i %s') AS time1,
time_format('14:57:40', '%h %i %s %p') AS time2,
time_format('14:57:40', '%H %I %S') AS time3,
time_format('14:57:40', '%r') AS time4;
Output
The above output displays four columns for display different time formats. If you use the uppercase expression, then time displays in 24-hour format. If time uses the lowercase expression, then the time function returns the 12-hour format. The "%r" and "%p" expressions show time is either AM or PM. The "%h", "%i", and "%s" shows hour, minute, and second respectively.