필터 지우기
필터 지우기

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

조회 수: 8 (최근 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