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 | CONVERT_TZ method

schedule Aug 12, 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 CONVERT_TZ(~) method converts a datetime value from one timezone to another.

Parameters

1. datetime | datetime

The datetime to convert from one timezone to another.

2. from_tz | timezone

The original timezone to convert from.

3. to_tz | timezone

The timezone to convert to.

Return value

The datetime value converted to the timezone passed as to_tz argument.

Examples

Basic usage

To convert current local time in Tokyo to EST (Eastern Standard Time):

SELECT NOW(), CONVERT_TZ(NOW(), 'Asia/Tokyo','America/New_York');
+---------------------+----------------------------------------------------+
| NOW() | CONVERT_TZ(NOW(), 'Asia/Tokyo','America/New_York') |
+---------------------+----------------------------------------------------+
| 2020-05-19 22:32:17 | 2020-05-19 09:32:17 |
+---------------------+----------------------------------------------------+

We convert the current time in Tokyo to equivalent time in New York.

To convert time using offset format:

SELECT CONVERT_TZ('2020-04-26 17:32:11', '+08:00', '-05:00');
+-------------------------------------------------------+
| CONVERT_TZ('2020-04-26 17:32:11', '+09:00', '-05:00') |
+-------------------------------------------------------+
| 2020-04-26 04:32:11 |
+-------------------------------------------------------+

Here +08:00 represents Tokyo time while -05:00 represents New York time. These are both offsets from UTC (Coordinated Universal Time).

Troubleshooting

Linux and macOS

If the above queries in the example return NULL, make sure you have loaded the time zone table into mysql. The MySQL installation procedure creates the time zone tables, but does not load them. Hence we have to load them manually by running the below in a terminal command line:

mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql

Here, note the following:

  • mysql_tzinfo_to_sql is the program that loads information to the time zone tables.

  • /usr/share/zoneinfo is where we fetch the set of files describing time zones.

  • -u is the flag for username. The account must have privileges for modifying tables in the mysql system schema.

  • -p is the flag for password.

If you face an error such as:

Warning: Unable to load '/usr/share/zoneinfo/+VERSION' as time zone.

You can override this by adding the force switch:

mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql --force

Windows

If the above queries in the example return NULL, you will need to proceed with the following steps to load information into the time zone tables in mysql:

1. Download the time zone package files from https://dev.mysql.com/downloads/timezones.html

2. Unpack the files and move them to your MySQL directory (something like C:\ProgramData\MySQL\MySQL Server 8.0\data\mysql)

3. Load the time zone information in the files using the below command:

mysql -u root -p mysql < file_name

Here, note the following:

  • -u is the flag for username. The account must have privileges for modifying tables in the mysql system schema.

  • -p is the flag for password.

  • file_name is the name of the unpacked file from step 2.

4. Restart your MySQL server.

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!