How to Use the MySQL NOW() Function
In this tutorial, we'll learn how to use the MySQL NOW() function. NOW() is a function to return the current date and time.
MySQL NOW() Syntax
The syntax of the NOW() function is as follows:
NOW()
Remarks
- The date and time is returned as "YYYY-MM-DD HH-MM-SS"
Examples
Example 1
The following statement returns the current and time:
SELECT NOW();
The current date and time is returned as "YYYY-MM-DD HH-MM-SS ", for example, 2019-05-11 13:29:19.
Example 2
The following statement returns the current date and time + 22 days:
SELECT DATE_ADD(NOW(), INTERVAL 22 DAY);
Here, we used the DATE_ADD function to add 22 days to the current date and time returned from the NOW() function. So, for example, if the current date and time is 2019-05-11 13:40:10, then this statement returns 2019-06-02 13:40:10.
Example 3
The following statement returns the time from the current data and time:
SELECT TIME(NOW());
Here, we used the TIME() function to extract the time from the current date and time. For example, if the current date and time is 2019-05-11 13:42:30, then this statement returns 13:42:30.
Example 4
The following statement returns the date from the current data and time:
SELECT DATE(NOW())
Here, we used the DATE function to extract the date from the current date and time. For example, if the current date and time is 2019-05-11 13:42:30, then this statement returns 2019-05-11.
Example 4
The following statement returns the current date from the current date and time, returned as MM/DD/YYYY:
SELECT DATE_FORMAT(NOW(),"%m/%d/%Y");
Here, we used the DATE_FORMAT function to extract the current date from the NOW() function and then format the returned date as MM/DD/YYYY. This means that if the current date and time is 2019-05-11 13:51:40, then this statement returns 05/11/2019.
In this tutorial, we've learned how to use the MySQL NOW() function. You use the NOW() function when you want to return the current date and time.