MATLAB: Can I get a datetime output in a different Time Zone from the input Time Zone in one function call

datetimeMATLAB

d = datetime('2016-Jan-20 12:00:00','TimeZone','UTC')
and then
d.TimeZone = 'America/New_York'
gets me the transformed time from the input… is there any way to get this answer in the original function call, without haveing the 2nd command? I know there is a Format option, but I cannot see how one can add a TimeZone in that. transform the input TimeZone.

Best Answer

d = subsasgn(datetime('2016-Jan-20 12:00:00','TimeZone','UTC'), struct('type', '.', 'subs', {'TimeZone'}), 'America/New_York');
You might want to create a help function for that, like
sset = @(var, sub, val) subsassgn(var, struct('type', '.', 'subs', {sub}), val);
and then
sset(datetime('2016-Jan-20 12:00:00','TimeZone','UTC'), 'TimeZone', 'America/New_York')
or
datetimeNY = @(date) subsasgn( datetime(date, 'TimeZone', 'UTC'), struct('type', '.', 'subs', {'TimeZone'}), 'America/New_York');
which you could then use as
d = datetimeNY('2016-Jan-20 12:00:00');