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

4 ビュー (過去 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 Center および File ExchangeDates and Time についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by