MySQL DATE_FORMAT() Function

Definition and Usage

The DATE_FORMAT() function is used to display date/time data in different formats.

Syntax

DATE_FORMAT(date,format)

date The parameter is a valid date.format Define the output format of the date/time.

The following formats can be used:

Format Description
%aAbbreviated name of the week
%bAbbreviated month name
%cMonth, a number
%DThe day of the month with English prefix
%dThe day of the month, a number (00-31)
%eDay of the month, numeric (0-31)
%fMicrosecond
%HHour (00-23)
%hHour (01-12)
%IHour (01-12)
%iMinute, numeric (00-59)
%jDay of the year (001-366)
%kHour (0-23)
%lHour (1-12)
%MMonth name
%mMonth, numeric (00-12)
%pAM or PM
%rTime, 12-hour (hh:mm:ss AM or PM)
%SSeconds (00-59)
%sSeconds (00-59)
%TTime, 24-hour (hh:mm:ss)
%U
㩵nWeek (00-53) Monday is the first day of the week
%VWeek (01-53) Sunday is the first day of the week, used with %X
%vWeek (01-53) Monday is the first day of the week, used with %x
%WDay name
%wDay of the week (0=Sunday, 6=Saturday)
%XYear in which Sunday is the first day of the week, 4 digits, used with %V
%xYear in which Monday is the first day of the week, 4 digits, used with %v
%YYear, 4 digits
%yYear, 2 digits

Example

The following script uses the DATE_FORMAT() function to display different formats. We use NOW() to get the current date/time:

DATE_FORMAT(NOW(),'%b %d %Y %h:%i %p')
DATE_FORMAT(NOW(),'%m-%d-%Y')
DATE_FORMAT(NOW(),'%d %b %y')
DATE_FORMAT(NOW(),'%d %b %Y %T:%f')

Results are similar:

Dec 29 2008 11:45 PM
12-29-2008
29 Dec 08
29 Dec 2008 16:25:46.635