How to convert table variable to datetime and adjust timezone?
14 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
casey
am 30 Apr. 2024
Verschoben: Cris LaPierre
am 2 Mai 2024
Hi there! I have a table that reads a csv file for which column A is "Time" and is in posix-millisec format. I would like to convert the values to dd-mm-yyyy format and then convert values into the time zone which the data were taken. I would like these newly formatted versions of the values to replace the originals in my table. Here is what I have put together so far.
My Table is name "T":
The following line works fine and the resultant dates are at UTC time:
T.Time = datetime(T.Time,'ConvertFrom','epochtime','TicksPerSecond',1e3,'Format','dd-MMM-yyyy HH:mm:ss.SSS')
The following line appears to work, but I am not sure how to check, and I think tells Matlab that the table variable Time is in UTC timezone so that it can be converted with subsequent commands.
T.Time = datetime(T.Time, "TimeZone","UTC")
This is where I get lost: How can I convert all of the values in T.Time to be in 'America/New_York' timezone so that when I plot T.Time vs <Stuff> my time axis reads in New York time?
I tried the following but it throws an error "Error using . To assign to or create a variable in a table, the number of rows must match the height of the table."
T.Time = 'America/New_York'
Thank you!
0 Kommentare
Akzeptierte Antwort
Cris LaPierre
am 30 Apr. 2024
For changing time zones, follow this example: https://www.mathworks.com/help/matlab/matlab_prog/specify-time-zones.html#SpecifyTimeZonesExample-4
I'm assuming the intial conversion to datetime is correct
Time = 1714509608*1e3; % milliseconds since 1/1/1970 0:00:00 UTC
T = table(Time);
T.Time = datetime(T.Time,'ConvertFrom','epochtime','TicksPerSecond',1e3,'Format','dd-MMM-yyyy HH:mm:ss.SSS')
% Assign time zone
T.Time.TimeZone = "UTC"
T.Time.TimeZone
% Convert to new Time Zone
T.Time.TimeZone = "America/New_York"
T.Time.TimeZone
2 Kommentare
Cris LaPierre
am 30 Apr. 2024
You could simplify a little by specifying the time zone as a name-value pair in datetime
Time = 1714509608*1e3;
T = table(Time);
T.Time = datetime(T.Time,'ConvertFrom','epochtime','TicksPerSecond',1e3,'Format','dd-MMM-yyyy HH:mm:ss.SSS',...
TimeZone="UTC")
% Check timezone
T.Time.TimeZone
% Now convert timezone
T.Time.TimeZone = "America/New_York"
% Check timezone
T.Time.TimeZone
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!