November 20, 2020

How to Use MySQL Date and Time Functions

Follow this step by step easy tutorial to learn how to use MySQL date and time functions and see how to modify the time formats.

Don’t forget to check out our site for more free how-to videos!
– our feed
– join us on facebook
– our group in Google+

There are various functions which can be used for calculating the date and time in MySQL but for the purpose of this tutorial we will use 3 primary functions for date and time each.
For this tutorial, we will be using SQL YOG as an instance of MySQL. 
Step 1- Current Date
Let’s start with MySQL date functions.
For that write the following query:
select curdate();
and press F5 to execute. It will show the current date of the system. Curdate is a short form of current date.

Step 2-Changing date format
Now if we want to view the date in a specific format, we can use a function called the date_format().
Write “date_format” function in a similar pattern as before, but this time inside the parentheses, we will write the date within single quotation marks and then specify the required format in single quotations after a comma.
Here we are using W for week, M for Month and Y for years which are implicit format specified in the MySQL environment, and used with a percentage sign.
The actual query would be:
select date_format(‘2013-04-18′,’%W %M %Y’);

Step 3- Viewing Name of the Day
In MySQL date functions library, there is a method which can return the day name, when given the date as its perimeter.
It is called “dayname” function. To use that, write dayname(), following the “select” keyword, and specify the date within single quotes inside the parenthesis.
The query would be:
select dayname(‘1998-04-25′);

Step 4- Time Functions
Now let’s move on to mysql time functions
First of all let’s find out the current time. For that, we will write curtime(),which stands for the current time, and write the query in the same way as we did for the current date.
Hence the query would be:
select curtime();

Step 5- Changing time format
Now let’s see how we can change the format of the output.
For that, use the time_format() function, as we did for the date format.
Write the following query in the editor:
select time_format(’14:28:18′,’%h : %i : %s’);
Over here, h, i and s are used for hours, minutes and seconds respectively.

Step 6- Changing Time to Seconds
In MyQL time functions library, there are various functions related to time. One of them which is widely used by programmers is the time_to_sec() function.
This method changes the given time to seconds.
To use this, write time_to_sec() and specify the time inside the parentheses within single quotes.
In the Query Editor, write:
select time_to_sec(’14:28:18′);
When we execute the query, we can see that the time being displayed is in seconds.

Step 7- Date along with Time
Lastly, to display the current date and time altogether, we can use now() function along with the select keyword.
select now();
in the query editor and execute it.

And this was all about MySQL date & time functions.


Xem thêm bài viết khác:

Related Posts

N.O.V.A. 3: Near Orbit Vanguard Alliance | Android | Playthrough

June 20, 2020

June 20, 2020 39

N.O.V.A. 3: Freedom Edition (Android) SO/OS: 6.0.1 (Marshmallow) Dispositivo/Device: Nvidia Tablet K1 Desarrollador/Developer: Gameloft Distribuidor/Publisher: Gameloft Versión/Version: 1.0.8e Lanzamiento/Launch: 15/12/2014...

Trên tay Galaxy Note FE – Chiếc điện thoại đẹp nhất trong lòng của mình!

June 5, 2020

June 5, 2020 22

Trên tay Galaxy Note FE – Chiếc điện thoại đẹp nhất trong lòng của mình! Group Tai nghe TWS Việt...

Cordova – Fix| NPM | Cordova | Node is not recognized as internal or external command in Windows

June 21, 2020

June 21, 2020

#Cordova #Fix #CordovaError #NpmNotRecognised #cordovaNotRecognised #AndroidHabridAppDevelopment #HybridAppDevelopment #NodeJs #NPM #Cordova Hi Guys, This is basic session for Developing Hybrid Android...

Mình thích và ghét điểm gì trên Galaxy Note 9 máy Mỹ?

June 13, 2020

June 13, 2020 17

► Chi tiết sản phẩm trong video xem tại đây: – Galaxy Note 9 Mỹ: 🌟 SUBSCRIBE CHANNEL: ——————————————————————— 🔰...

Galaxy Note 9 ra mắt: Không có gì đặc biệt?

June 13, 2020

June 13, 2020 45

Galaxy Note 9 ra mắt: Không có gì đặc biệt? Đúng như nhiều thông tin được rỏ rì từ trước,...

Cách tạo ghi chú trên desktop Win 10 – Tạo note trên Win 10

June 9, 2020

June 9, 2020 6

Cách tạo ghi chú trên desktop Win 10, cách tạo note trên desktop Win 10 để ghi nhớ công việc,...

3 DIY Journal Design Ideas | Notebook designs | AiiMADEit

June 10, 2020

June 10, 2020 18

Hello you guys! ❤ Here’s a 3 easy and unique designs you can make for your journals or notebooks! The...

So sánh Galaxy Note 9 Mỹ và Note 9 Hàn!

June 17, 2020

June 17, 2020 35

Liệu giữa #galaxy_note9_mỹ và #note9_hàn có những điểm gì khác nhau và bạn thích xài máy nào hơn? ► Chi...

Unboxing Samsung Galaxy Note FE Indonesia!

June 22, 2020

June 22, 2020 34

Flat earth? fire extinguisher? atau singkatan yang lain? Instagram GadgetIn: Twitter GadgetIn: Facebook GadgetIn: HP ini bisa dibeli di: HP...

Tạo ghi chú với Sticky Notes trên Windows 10

June 9, 2020

June 9, 2020

► Kênh TTGit : Hướng dẫn chi tiết từ A-Z, chia sẻ kiến thức, kinh nghiệm sử dụng phần mềm...

Galaxy Note 9 Hàn sau 1 năm có bị LAG?

June 12, 2020

June 12, 2020 27

Tham khảo giá bán của Galaxy Note 9 tại: 🔗 Mua dán da, bao da cho smartphone tại 🔗 Galaxy...

Galaxy Note 7 FE kutusundan çıkıyor – Note 7 ile hasret giderdik!

June 19, 2020

June 19, 2020 50

Samsung’un piyasadan çektiği Galaxy Note 7 modelinin yenilenmiş ve güvenilir sürümü olan ve Türkiye’de resmen satışa sunulmayan Galaxy Note 7...

Notepad++ Discover The Best Features and Plugins

June 20, 2020

June 20, 2020 35

Upgrade your Clever Techie learning experience: UPDATE! (9/13/19) New features and improvements for Clever Techie Patreons: 1. Download full source...

Review Notebook Samsung Flash F30 | Análise Completa básico e barato tela 13” Full HD – Roda jogos?

June 24, 2020

June 24, 2020 50

+ Detalhes no post ►► Chegamos ao esperado vídeo review do Samsung Flash, que também é chamado de F30. Um...

1 tuần với Samsung Galaxy Note9 – Chiếc Flagship được nâng cấp để hoàn hảo?!

June 13, 2020

June 13, 2020 31

Samsung Galaxy Note9 – một chiếc flagship thật sự với cấu hình khủng, cây bút S-Pen được nâng cấp và…...

  • ❗ Subscribe To Our Main YouTube Channel

  • short and accurate ! Great video, only thing you need is to zoom in the code to read what you're talking about, that would make things a lot easier

  • Is it possible to use the "now()" method to insert the current time and date to a text type variable for example? Sorry for the silly question, there are some things in MySQL I'm still learning.

Leave a Reply

Your email address will not be published. Required fields are marked *