Main Content

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
   2024-07-20

instead of this:

t = datetime('today','Format','eeee, dd-MMM-yyyy')
t = datetime
   Saturday, 20-Jul-2024

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
   14:43

instead of this:

t = datetime('now','Format','hh:mm a')
t = datetime
   02:43 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
   20-07-2024 -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
   20-Jul-2024
   21-Jul-2024

S = cellstr(t,'dd. MMMM yyyy','de_DE')
S = 2x1 cell
    {'20. Juli 2024'}
    {'21. Juli 2024'}

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

| | | |