How can I set the time zone of a datetime series using a duration value as the UTC offset?

13 views (last 30 days)
I have a value stored as a duration object, and I would like to use this to specify the time zone of some datetime data, in terms of offset from UTC. However, it does not allow me to specify the 'TimeZone' property of the datetime series with a numeric or duration value, and I am not able to change the 'Format' property of the duration to a valid time zone format. I would prefer to be able to do this any of the following ways:
% Set the time zone with the duration object directly
t.TimeZone = durationVal;
% Specify the duration object display format (automatically)
durationVal.Format = 'XXX';
t.TimeZone = char(durationVal);
% Specify duration object display format (manually)
durationVal.Format = '+HH:mm';
t.TimeZone = char(durationVal);
% Use a numeric value to specify the time zone
t.TimeZone = hours(durationVal);

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 4 Feb 2016
Edited: MathWorks Support Team on 4 Feb 2016
There is an enhancement request in-place for adding more options to the 'TimeZone' property of the datetime series, and/or the 'Format' property of the duration object.
As a workaround, formatting strings manually may be the best option. For example:
% Create sample data
DateStrings = {'2014-05-26T13:30-05:00';'2014-08-26T13:30-04:00';'2014-09-26T13:30Z'};
t = datetime(DateStrings,'InputFormat','uuuu-MM-dd''T''HH:mmXXX','TimeZone','UTC')
dOffset = duration(-4,0,0);
% Create a string from the duration in valid time zone format
tZone = sprintf('%+03d00',hours(dOffset));
% Set the time zone with the string
t.TimeZone = tZone

More Answers (0)

Categories

Find more on Dates and Time in Help Center and File Exchange

Products


Release

No release entered yet.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!