MySQL TIME_FORMAT() Function

In MySQL, the TIME_FORMAT() function formats the time according to the specified format.

TIME_FORMAT() Syntax

Here is the syntax of MySQL TIME_FORMAT() function:

TIME_FORMAT(time, format)

Parameters

time
Required. The time to format.
format
Required. The format string.

The specifiers shown in the following table may be used in the format string.

Placeholder illustrate
%f microseconds ( 000000.. 999999)
%H hours (00.. 23)
%h hours (01.. 12)
%I hours (01.. 12)
%i minutes (00.. 59)
%k hours (0.. 23)
%l hours (1.. 12)
%p AM or PM
%r Twelve-hour time (hh:mm:ss followed by AM or PM)
%S seconds (00.. 59)
%s seconds (00.. 59)
%T Twenty-four hour time (hh:mm:ss)
%% escape %

Return value

The TIME_FORMAT() function formats the time according to the specified format and returns the result as a string.

The TIME_FORMAT() function will return NULL if any of the arguments are NULL.

TIME_FORMAT() Examples

Here are some examples of the TIME_FORMAT() function.

SELECT
    TIME_FORMAT("19:30:10", "%H %i %s"),
    TIME_FORMAT("19:30:10", "%h %i %s %p"),
    TIME_FORMAT("19:30:10", "%r"),
    TIME_FORMAT("19:30:10", "%T"),
    TIME_FORMAT("19:30:10", "%H %i %s")\G
   TIME_FORMAT("19:30:10", "%H %i %s"): 19 30 10
TIME_FORMAT("19:30:10", "%h %i %s %p"): 07 30 10 PM
         TIME_FORMAT("19:30:10", "%r"): 07:30:10 PM
         TIME_FORMAT("19:30:10", "%T"): 19:30:10
   TIME_FORMAT("19:30:10", "%H %i %s"): 19 30 10