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

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

采纳的回答

Walter Roberson
Walter Roberson 2017-1-23
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');
  1 个评论
Jeff Waldron
Jeff Waldron 2017-1-23
Walter,
Thank you... that's great... I have seen subsasgn in a couple other answers on other topics, but I've never used it, and thus always forget about it's capabilitities... Appreciate the direction.

请先登录,再进行评论。

更多回答(2 个)

Peter Perkins
Peter Perkins 2017-1-23
I think this also does what you want:
>> d = datetime('2016-Jan-20 12:00:00 UTC','InputFormat','yyyy-MMM-dd HH:mm:ss z','TimeZone','America/New_York')
d =
datetime
20-Jan-2016 07:00:00
but it requires that 'UTC' be appended to each timestamp. So choose your poison: is calling subsasgn directly better or worse? The only reason I can think to need one line is that you're writing an anonymous function.

Maitreyee Mordekar
Maitreyee Mordekar 2017-1-23
Hi Jeff,
The following command will help you set the desired functionality-
d = datetime('2016-Jan-20 12:00:00','TimeZone','America/New_York');
The following link is the reference documentation for 'datatime' function-
  2 个评论
Walter Roberson
Walter Roberson 2017-1-23
That does not quite do what Jeff needs. Jeff needs to create a time in UTC and then shift it to New York timezone. The time that would be created by the command you show would differ by 5 hours from the time created by the commands that Jeff shows.
Peter Perkins
Peter Perkins 2017-1-23
Just to expand a bit on this:
If you assign the TimeZone property of an unzoned datetime, the result will "look" the same, in other words, it becomes the same clockface time in a specific time zone.
On the other hand, if you assign the TimeZone property of a datetime that's already in a specific time zone, the instant in time remains the same, but it "looks" different because of the different time zone offsets. So what Walter is saying is that creating a datetime at 12pm in UTC, and then setting its TimeZone property to America/New_York results in 7am, New York time. Same instant, different timestamp.

请先登录,再进行评论。

类别

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