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

4 visualizaciones (últimos 30 días)
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.

Respuesta aceptada

Walter Roberson
Walter Roberson el 23 de En. de 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 comentario
Jeff Waldron
Jeff Waldron el 23 de En. de 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.

Iniciar sesión para comentar.

Más respuestas (2)

Peter Perkins
Peter Perkins el 23 de En. de 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 el 23 de En. de 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 comentarios
Walter Roberson
Walter Roberson el 23 de En. de 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 el 23 de En. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Dates and Time en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by