The date function used to parse a date or datetime value, according to a given format string. A wide variety of parsing options are available.
parse() The Date. parse() method parses a string representation of a date, and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC or NaN if the string is unrecognized or, in some cases, contains illegal date values (e.g. 2015-02-31). Only the ISO 8601 format ( YYYY-MM-DDTHH:mm:ss.
The MySQL DATE_FORMAT() function formats a date value with a given specified format. You may also use MySQL DATE_FORMAT() on datetime values and use some of the formats specified for the TIME_FORMAT() function to format the time value as well. Let us take a look at the syntax of DATE_FORMAT() and some examples.
MySQL STR_TO_DATE() Function The STR_TO_DATE() function returns a date based on a string and a format.
You may want to use the STR_TO_DATE() function. It's the inverse of the DATE_FORMAT() function.
STR_TO_DATE(str,format)
This is the inverse of the
DATE_FORMAT()function. It takes a stringstrand a format stringformat.STR_TO_DATE()returns aDATETIMEvalue if the format string contains both date and time parts, or aDATEorTIMEvalue if the string contains only date or time parts. If the date, time, or datetime value extracted fromstris illegal,STR_TO_DATE()returnsNULLand produces a warning.
Example:
SELECT STR_TO_DATE('15-Dec-09', '%d-%b-%y') AS date;
+------------+
| date       |
+------------+
| 2009-12-15 |
+------------+
1 row in set (0.00 sec)
    Here's a table of format %-codes used in DATE_FORMAT() and STR_TO_DATE().
     -----examples-for------
     1999-12-31   2000-01-02
     23:59:58.999 03:04:05      identical to
     ------------ ----------    -------------
 %a  Fri          Sun      LEFT(DAYNAME(d),3)
 %b  Dec          Jan      LEFT(MONTHNAME(d),3)
 %c  12           1             MONTH(d)            
 %D  31st         2nd           DAYOFMONTH(d)+st,nd,rd
 %d  31           02       LPAD(DAYOFMONTH(d),0,2)
 %e  31           2             DAYOFMONTH(d)     
 %f  999000       000000   LPAD(MICROSECOND(t),6,0)
 %H  23           03       LPAD(HOUR(t),2,0)
 %h  11           03
 %I  11           03
 %i  59           04       LPAD(MINUTE(t),2,0)
 %j  365          002
 %k  23           3             HOUR(t)             
 %l  11           3
 %M  December     January       MONTHNAME(d)  
 %m  12           01       LPAD(MONTH(d),2,0) 
 %p  PM           AM
 %r  11:59:58 PM  03:04:05 AM
 %S  58           05       LPAD(SECOND(t),2,0)
 %s  58           05       LPAD(SECOND(t),2,0)
 %T  23:59:58     03:04:05
 %U  52           01       LPAD(WEEK(d,0),2,0)
 %u  52           00       LPAD(WEEK(d,1),2,0)
 %V  52           01      RIGHT(YEARWEEK(d,2),2)
 %v  52           52      RIGHT(YEARWEEK(d,3),2)
 %W  Friday       Sunday        DAYNAME(d)
 %w  5            0             DAYOFWEEK(d)-1
 %X  1999         2000     LEFT(YEARWEEK(d,2),4)
 %x  1999         1999     LEFT(YEARWEEK(d,3),4)
 %Y  1999         2000          YEAR(d)   
 %y  99           00      RIGHT(YEAR(d),2)
 %%  %            %
or
%X%V 199952       200001        YEARWEEK(d,2)
%x%v 199952       199952        YEARWEEK(d,3)
By the way there are no %-codes for unpadded minutes or seconds:
     59           4             MINUTE(t)  
     58           5             SECOND(t)
In action:
 '15-Dec-09' == DATE_FORMAT('2009-12-15', '%d-%b-%y')
 '2009-12-15' == STR_TO_DATE('15-Dec-09', '%d-%b-%y')
    
                If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With