This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Share Code and Data Across Locales

Write Locale-Independent Date and Time Code

Follow these best practices when sharing code that handles dates and time with MATLAB® users in other locales. These practices ensure that the same code produces the same output display and that output files containing dates and time are read correctly on systems in different countries or with different language settings.

Create language-independent datetime values. That is, create datetime values that use month numbers rather than month names, such as 01 instead of January. Avoid using day of week names.

For example, do this:

t = datetime('today','Format','yyyy-MM-dd')
t = datetime

instead of this:

t = datetime('today','Format','eeee, dd-MMM-yyyy')
t = datetime
   Friday, 01-Sep-2017

Display the hour using 24-hour clock notation rather than 12-hour clock notation. Use the 'HH' identifiers when specifying the display format for datetime values.

For example, do this:

t = datetime('now','Format','HH:mm')
t = datetime

instead of this:

t = datetime('now','Format','hh:mm a')
t = datetime
   04:04 PM

When specifying the display format for time zone information, use the Z or X identifiers instead of the lowercase z to avoid the creation of time zone names that might not be recognized in other languages or regions.

Assign a time zone to t.

t.TimeZone = 'America/New_York';

Specify a language-independent display format that includes a time zone.

t.Format = 'dd-MM-yyyy Z'
t = datetime
   01-09-2017 -0400

If you share files but not code, you do not need to write locale-independent code while you work in MATLAB. However, when you write to a file, ensure that any text representing dates and times is language-independent. Then, other MATLAB users can read the files easily without having to specify a locale in which to interpret date and time data.

Write Dates in Other Languages

Specify an appropriate format for text representing dates and times when you use the char or cellstr functions. For example, convert two datetime values to a cell array of character vectors using cellstr. Specify the format and the locale to represent the day, month, and year of each datetime value as text.

t = [datetime('today');datetime('tomorrow')]
t = 2x1 datetime array

S = cellstr(t,'dd. MMMM yyyy','de_DE')
S = 2x1 cell array
    {'01. September 2017'}
    {'02. September 2017'}

S is a cell array of character vectors representing dates in German. You can export S to a text file to use with systems in the de_DE locale.

Read Dates in Other Languages

You can read text files containing dates and time in a language other than the language that MATLAB® uses, which depends on your system locale. Use the textscan or readtable functions with the DateLocale name-value pair argument to specify the locale in which the function interprets the dates in the file. In addition, you might need to specify the character encoding of a file that contains characters that are not recognized by your computer's default encoding.

  • When reading text files using the textscan function, specify the file encoding when opening the file with fopen. The encoding is the fourth input argument to fopen.

  • When reading text files using the readtable function, use the FileEncoding name-value pair argument to specify the character encoding associated with the file.

See Also

| | | |

Was this topic helpful?