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

8 次查看(过去 30 天)
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);

采纳的回答

MathWorks Support Team
编辑:MathWorks Support Team 2016-2-4
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

产品


版本

尚未输入任何版本。

Community Treasure Hunt

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

Start Hunting!

Translated by