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

4 Ansichten (letzte 30 Tage)
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.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 23 Jan. 2017
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 Kommentar
Jeff Waldron
Jeff Waldron am 23 Jan. 2017
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Peter Perkins
Peter Perkins am 23 Jan. 2017
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 am 23 Jan. 2017
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 Kommentare
Walter Roberson
Walter Roberson am 23 Jan. 2017
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 am 23 Jan. 2017
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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Dates and Time finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by