In view of the fact that the conversion of the date format is often used to provide the front-end with the correct time display format, there are many formatting methods, such as the Java method, as follows:
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(date);
But when we encounter multiple fields, this method will undoubtedly redundant a lot of codes. It is a better solution to choose to format the date format when querying in SQL.
Let’s talk about the date_format() function today. The specific usage is as follows:
date_format(): Function used in mysql to display various formats of time\date
Format: DATE_FORMAT(date, format) date: date format: format
Example:
SELECT DATE_FORMAT(SYSDATE(),'%Y-%m-%d %H:%i%s') from dual;
Detailed format format:
Format | describe |
---|---|
%a | abbreviated day of the week |
%b | abbreviated month name |
%c | month, value |
%D | day of month with english prefix |
%d | Day of the month, value (00-31) |
%e | Day of the month, value (0-31) |
%f | microsecond |
%H | hour (00-23) |
%h | hour (01-12) |
%I | hour (01-12) |
%i | minute, value (00-59) |
%j | Day of the year (001-366) |
%k | hour(0-23) |
%l | hours (1-12) |
%M | month name |
%m | month, value (00-12) |
%p | AM or PM |
%r | Time, 12-hour (hh:mm:ss AM or PM) |
%S | seconds (00-59) |
%s | seconds (00-59) |
%T | Time, 24-hour (hh:mm:ss) |
%U | Week (00-53) Sunday is the first day of the week |
%u | Week (00-53) Monday is the first day of the week |
%V | Week (01-53) Sunday is the first day of the week, used with %X |
%v | Week (01-53) Monday is the first day of the week, use with %x |
%W | week name |
%w | Day of the week (0=Sunday, 6=Saturday) |
%X | Year, where Sunday is the first day of the week, 4 digits, used with %V |
%x | Year, where Monday is the first day of the week, 4 digits, used with %v |
%Y | year, 4 digits |
%y | year, 2 digits |
The above is a basic detailed explanation of the usage of this function! ! !