How to Get the Current Date and Time in MySQL

08/05/2021

Contents

In this article, you will learn how to get the current date and time in MySQL.

Getting the current date and time in MySQL

In MySQL, there are several ways to get the current date and time. Here are some common methods with explanations and examples:

Using the NOW() function

The NOW() function in MySQL returns the current date and time in the format ‘YYYY-MM-DD HH:MM:SS’.

Syntax

SELECT NOW();

Example

SELECT NOW();

Output:

+---------------------+
| NOW()               |
+---------------------+
| 2023-03-30 16:23:45 |
+---------------------+

Using the CURRENT_TIMESTAMP() function

The CURRENT_TIMESTAMP() function in MySQL returns the current date and time in the format ‘YYYY-MM-DD HH:MM:SS’.

Syntax

SELECT CURRENT_TIMESTAMP();

Example

SELECT CURRENT_TIMESTAMP();

Output:

+---------------------+
| CURRENT_TIMESTAMP() |
+---------------------+
| 2023-03-30 16:23:45 |
+---------------------+

Using the SYSDATE() function

The SYSDATE() function in MySQL returns the current date and time in the format ‘YYYY-MM-DD HH:MM:SS’.

Syntax

SELECT SYSDATE();

Example

SELECT SYSDATE();

Output:

+---------------------+
| SYSDATE()           |
+---------------------+
| 2023-03-30 16:23:45 |
+---------------------+

Using the CURRENT_DATE() function and the CURRENT_TIME() function

To get the current date and time separately, you can use the CURRENT_DATE() function to get the current date and the CURRENT_TIME() function to get the current time.

Syntax

SELECT CURRENT_DATE(), CURRENT_TIME();

Example

SELECT CURRENT_DATE(), CURRENT_TIME();

Output:

+--------------+-------------+
| CURRENT_DATE | CURRENT_TIME|
+--------------+-------------+
| 2023-03-30   | 16:23:45    |
+--------------+-------------+

Using the DATE_FORMAT() function

The DATE_FORMAT() function in MySQL allows you to format the current date and time in a custom way.

Syntax

SELECT DATE_FORMAT(NOW(), 'format_string');

Example

SELECT DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%s');

Output:

+-------------------------+
| DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%s') |
+-------------------------+
| 2023-03-30 16:23:45      |
+-------------------------+