MySQL DateTime Tutorial
In this tutorial, I am going to explain MySQL DATE and TIME functions with examples.
DATETIME is used to store the value of both the date and time. By default, DATETIME values ranges from 1000-01-01 00:00:00 to 9999-12-31 23:59:59. It uses the 5 bytes for storage.
Syntax for DATETIME format
YYYY-MM-DD HH:MM:SS
MySQL DATE and TIME Types
There are a number of useful date and time functions in MySQL. I've seen too many applications performing date calculations at the code level when the same can be done using built-in MySQL functions. Before we launch into the functions, however, let's refresh our memory and look at which date and time types are available to MySQL.
DATETIME |
YYYY-MM-DD HH:MM:SS |
DATE |
YYYY-MM-DD |
TIMESTAMP |
YYYYMMDDHHSSMM |
TIME |
HH:MM:SS |
YEAR |
YYYY |
MySQL DATETIME
MySQL is quite lenient in how it reads date formats. Although it is wise to use the convention, you can use any other punctuation character you like. For example, now create a table and insert the dummy data as follows.
CREATE TABLE vatsa(Dt DATETIME);
Now, insert the data in the right format as follows.
INSERT INTO vatsa(Dt)VALUES('2020-09-1423:18:17');
Note.
Instead of the right format, you can use '=' and '+' signs, as follows.
INSERT INTO vatsa(Dt)VALUES('2020=09=1423*18*21');
But although I'm sure they exist, I haven't come across a good reason to use this, so I suggest you keep to the conventions unless absolutely necessary.
Now, execute the following query to see the result.
-
SELECT*FROM&vats;
SIMPLE DATE and TIME CALCULATION
Date calculations are relatively easy. The first function we're going to look at is the YEAR() function, which returns a year from a given date. To get the value of the year, month, week, day, quarter, date, time, hour, minute, and second from aDATETIME
value, you use the functions as shown in the following statement.
For example
- SELECTYEAR('2020-09-1423:18:17')ASYear,
- Month('2020-09-1423:18:17')ASMonth,
- Day('2020-09-1423:18:17')ASDay,
- Hour('2020-09-1423:18:17')ASHour,
- Minute('2020-09-1423:18:17')ASMinute,
- Second('2020-09-1423:18:17')ASSecond,
- Date('2020-09-1423:18:17')ASDate,
- Time('2020-09-1423:18:17')ASTime,
- Quarter('2020-09-1423:18:17')ASQuarter;
Alternatively, the user can use a dynamic method to fetch the date and time of the current datetime.
- SET@vatsaDATETIME=NOW();
-
- SELECTYEAR(@vatsaDATETIME)ASYear,
- Month(@vatsaDATETIME)ASMonth,
- Day(@vatsaDATETIME)ASDay,
- Hour(@vatsaDATETIME)ASHour,
- Minute(@vatsaDATETIME)ASMinute,
- Second(@vatsaDATETIME)ASSecond,
- Date(@vatsaDATETIME)ASDate,
- Time(@vatsaDATETIME)ASTime,
- Quarter(@vatsaDATETIME)ASQuarter;
BASIC ARITHMETIC OPERATIONS ON DATE FUNCTION
We can perform simple arithmetic operations on a date using the '+' and '-' operators. Lets us see some basic examples.
For example.
1) To find out which year is five years ahead of a given date, you can use it.
SET@vatsa=now();
SELECT@vatsa+5;
2) To find out which year was five years in the past, use.
SET@vatsa=now();
SELECT@vatsa–5;
Now, let’s discuss a basic real-time example.
-
SET@vatsa=now();
SELECT@vatsa-5ASPAST,
year(@vatsa)ASCURRENT,
@vatsa+5ASFUTURE;
NOW() Function
Of course, you don't have to hard-code the date. MySQL is quite capable of telling the date and time, using the NOW() function. And, on the execution of this statement, it returns the current date and time.
- SELECTNOW();
CURRENT_DATE() Function
Or just the date with the CURRENT_DATE() function. And, on the execution of this statement, it returns the current date.
- SELECTCURRENT_DATE();
MySQL DATETIME vs TIMESTAMP
TIMESTMP is similar to DATTIME in MySQL. TIMESTAMP requires 4 bytes, but DATETIME requires 5 bytes.
The TIMESTAMP column stores the full 14 characters, but you can display it in different ways. If you define the column as TIMESTAMP(2), for example, only the two-digit year will be displayed, but the full value is stored. If you later decide to display the full value, you can change the table definition, and the full value will appear.
Below is a list of various ways to define a TIMESTAMP and the resultant display.
TIMESTAMP(14) |
YYYYMMDDHHMMSS |
TIMESTAMP(12) |
YYMMDDHHMMSS |
TIMESTAMP(10) |
YYMMDDHHMM |
TIMESTAMP(8) |
YYYYMMDD |
TIMESTAMP(6) |
YYMMDD |
TIMESTAMP(4) |
YYMM |
TIMESTAMP(2) |
YY |
CONCLUSION
In this article, I have discussed the concept of DATE and TIME functions in MySQL with various examples.
I hope you enjoyed this article. Follow C# Corner to learn more new and amazing things about MySQL.
Thanks for reading this article!