MySql string and time conversion

1. String to time

SELECT STR_TO_DATE("2021-02-20", "%Y-%m-%d") FROM DUAL;

2. Time to string

SELECT DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%s') FROM DUAL;
 输出结果:2021-02-21 16:24:50

 

MySQL date formatting (format) value range

            Value meaning

 
Seconds %S, %s Two-digit seconds (00,01, ..., 59)
minutes %I, %i Two-digit minutes (00,01, ..., 59)
hours %H 24 Hour format, two-digit hour (00,01, ...,23)
            %h 12-hour format, two-digit hour (00,01, ...,12)
            %k 24-hour format, numeric hour (0,1, ...,23)
            %l 12-hour system, number form hour (0,1, ...,12)
            %T 24-hour system, time form (HH:mm:ss)
            %r 12 hours Time format (hh:mm:ss AM or PM)
            %p AM AM or PM PM 
 Week %W The name of each day of the week (Sunday,Monday, ...,Saturday)
           %a The abbreviation of the name of each day of the week (Sun,Mon, ...,Sat) 
           %w identifies the week as a number (0=Sunday,1=Monday, ...,6=Saturday) 
           %U The number represents the week number, and Sunday is the first day of the week
           % u The number represents the number of weeks, and Monday is the first day of the week.
Day %d Two digits represent the number of days in the month (01,02, ...,31)
          %e numbers represent the number of days in the month (1,2, ...,31)
          %D English suffix represents the number of days in the month (1st, 2nd, 3rd...) 
          %j represents the number of days in the year (001,002,. ..,366) 
Month %M English month name (January,February, ...,December) 
         %b English abbreviated month name (Jan,Feb, ...,Dec) 
         %m Two digits for month (01,02) , ...,12)
         %c number represents month (1,2, ...,12) 
year %Y year represented by four digits (2015,2016...)
         %y year represented by two digits (15 ,16...)
 
Text output% text output text content directly  

 

Guess you like

Origin blog.csdn.net/y_bccl27/article/details/113891261