search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to

MySQL | TIME_FORMAT method

schedule Aug 11, 2023
Last updated
local_offer
MySQL
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

MySQL's TIME_FORMAT(~) method will return a time formatted according to the provided format.

Parameters

1. time | time

The time to format.

2. format | string

The format to use for formatting.

Refer to 'List of formatting types' at the bottom of page.

Return value

Returns the formatted time.

Examples

Basic usage

To format the input time as 12-hour hh:mm:ss followed by AM or PM:

SELECT TIME_FORMAT('23:32:40', '%r');
+-------------------------------+
| TIME_FORMAT('23:32:40', '%r') |
+-------------------------------+
| 11:32:40 PM |
+-------------------------------+

The inputted 24-hour time has been returned as a 12-hour time with AM or PM.

Hour formatting

To compare the different types of formatting available for the hour portion 25:

SELECT TIME_FORMAT('25:00:00', '%H %k %h %I %l');
+-------------------------------------------+
| TIME_FORMAT('25:00:00', '%H %k %h %I %l') |
+-------------------------------------------+
| 25 25 01 01 1 |
+-------------------------------------------+

Note here that:

  • %H and %k hour format specifiers can produce values larger than 23. We see they return 25 here.

  • %h, %I, and %l hour format specifiers produce the hour value modulo 12. We see they return 01, 01 and 1 respectively here.

List of formatting types

Below is a table with a list of valid formatting types:

Specifier

Description

%f

Microseconds (000000..999999)

%H

Hour (00..23). Can produce value above 23 if required.

%h

Hour (01..12). For values greater than 12, the hour value modulo 12.

%I

Hour (01..12). For values greater than 12, the hour value modulo 12.

%i

Minutes, numeric (00..59)

%k

Hour (0..23). Can produce value above 23 if required.

%l

Hour (1..12). For values greater than 12, the hour value modulo 12.

%p

AM or PM

%r

Time, 12-hour (hh:mm:ss followed by AM or PM)

%S

Seconds (00..59)

%s

Seconds (00..59)

%T

Time, 24-hour (hh:mm:ss)

robocat
Published by Arthur Yanagisawa
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!