MySQL date_format convert date/datetime into string

DATE_FORMAT function converts a DATE or DATETIME value to string. DATE_FORMAT have two parameter, In first parameter you need to assign date or datetime, In second parameter assign date format syntex. Pass the specified format and you get the date in passed format.

NOW() is MySQL function which return the current date time. “%Y-%m” is format that you want to get, it return 2018-01.

DATE_FORMAT and TO_CHAR Format Specifiers

%Y 4-digit year
%y 2-digit year
%b Abbreviated month (Jan – Dec)
%M Month name (January – December)
%m Month (0 – 12)
%a Abbreviated day (Sun – Sat)
%d Day (0 – 31)
%H Hour (0 – 23)
%h Hour (1 – 12)
%i Minutes (0 – 59)
%s Seconds (0 – 59)
%t Time (hours, minutes and seconds)

Conversion Examples

MySQL Sample Output
DATE_FORMAT(NOW(), ‘%Y-%m-%d) 2013-02-14
DATE_FORMAT(NOW(), ‘%d/%m/%y’) 14/02/13
DATE_FORMAT(NOW(), ‘%d-%M-%y’) 14-February-13
DATE_FORMAT(NOW(), ‘%d/%m/%y %T’) 14/02/13 15:35:22


Was this post helpful?
Let us know if you liked the post. That’s the only way we can improve.
Harinder Singh

Harinder Singh

My name is Harinder Singh and I specialize in Software industry. I consider myself as a life learner. I love learning new concepts, embracing new ideas and reading and searching for innovation.