What is the correct DateTime format for a MySQL Database?


The correct datetime format for MySQL database is as follows −

‘YYYY-MM-DD HH:M:SS’

Let us first create a table −

mysql> create table DemoTable797 (
   ArrivalDatetime datetime
);
Query OK, 0 rows affected (0.68 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable797 values(NOW());
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable797 values('2016-12-21 12:50:34');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable797 values('2017-03-01 17:40:21');
Query OK, 1 row affected (0.24 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable797;

This will produce the following output. We have used NOW() while inserting the first value in the table. The NOW() displays the current date and time −

+---------------------+
| ArrivalDatetime     |
+---------------------+
| 2019-08-01 21:40:42 |
| 2016-12-21 12:50:34 |
| 2017-03-01 17:40:21 |
+---------------------+
3 rows in set (0.00 sec)

Updated on: 09-Sep-2019

153 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements