Specify Time Zones
In MATLAB®, a time zone includes the time offset from Coordinated
Universal Time (UTC), the daylight saving time offset, and a set of
historical changes to those values. The time zone setting is stored
in the TimeZone
property of each datetime
array.
When you create a datetime, it is unzoned by default. That is, the TimeZone
property
of the datetime is empty (''
). If you do not work
with datetime values from multiple time zones and do not need to account
for daylight saving time, you might not need to specify this property.
You can specify a time zone when you create a datetime, using
the 'TimeZone'
name-value pair argument. The time
zone value 'local'
specifies the system time zone.
To display the time zone offset for each datetime, include a time
zone offset specifier such as 'Z'
in the value
for the 'Format'
argument.
t = datetime(2014,3,8:9,6,0,0,'TimeZone','local',... 'Format','d-MMM-y HH:mm:ss Z')
t = 8-Mar-2014 06:00:00 -0500 9-Mar-2014 06:00:00 -0400
A different time zone offset is displayed depending on whether the datetime occurs during daylight saving time.
You can modify the time zone of an existing datetime. For example,
change the TimeZone
property of t
using
dot notation. You can specify the time zone value as the name of a
time zone region in the IANA Time Zone Database. A time zone region
accounts for the current and historical rules for standard and daylight
offsets from UTC that are observed in that geographic region.
t.TimeZone = 'Asia/Shanghai'
t = 8-Mar-2014 19:00:00 +0800 9-Mar-2014 18:00:00 +0800
You also can specify the time zone value as a character vector
of the form +HH:mm
or -HH:mm
,
which represents a time zone with a fixed offset from UTC that does
not observe daylight saving time.
t.TimeZone = '+08:00'
t = 8-Mar-2014 19:00:00 +0800 9-Mar-2014 18:00:00 +0800
Operations on datetime arrays with time zones automatically account for time zone differences. For example, create a datetime in a different time zone.
u = datetime(2014,3,9,6,0,0,'TimeZone','Europe/London',... 'Format','d-MMM-y HH:mm:ss Z')
u = 9-Mar-2014 06:00:00 +0000
View the time difference between the two datetime arrays.
dt = t - u
dt = -19:00:00 04:00:00
When you perform operations involving datetime arrays, the arrays either must all have a time zone associated with them, or they must all have no time zone.