How to convert Universal time to Local time?

Dear All,
I am trying to convert Universal time to local time. I used this function
% Convert time values to datetime array
timeDatetime = datetime(selectedData(:,3)./3600, 'ConvertFrom', 'epochtime', 'Epoch', '1970-01-01');
% Apply time zone offset to datetime array
timeLocal = tzoffset(timeDatetime, 'EST','5');
but I got an error " Too Many input arguments".
Does anyone knows how to resoolve it?
Thank you,
Ara

4 Comments

@Ara, tzoffset only takes one input. You should be able to "convert" timeDatetime to EST by just setting its TimeZone attribute to 'America/New_York' (if you want EST/EDT otherwise Etc/GMT+5 ) before referencing the data from it, as in the following. Note that the timeDatetime creation will assume your local TimeZone and therefore the setting of TimeZone to somthing that is the same will result in the same values being reported (e.g., selectedData(:,3)./3600 will be assumed to be in your local time so you would probably want to set it in the datetime call as indicated.)
% Convert time values to datetime array
timeDatetime = datetime(selectedData(:,3)./3600 ...
,'ConvertFrom', 'epochtime', 'Epoch', '1970-01-01' ...
,'TimeZone','Etc/UTC')
% Apply time zone offset to datetime array
timeDatetime.TimeZone = 'America/New_York'
Hi Jeffrey Clark,
Thank you for your useful explanation and the code.
What if I want the local time for AFrican, Asian, Pacific regions?
My time is in Universal time not local time but I want the plot in local time.
The figure is like this. I want it from 0-24 LT
dt = datetime('now', 'TimeZone', 'local')
dt = datetime
27-Dec-2022 14:55:52
dt.TimeZone
ans = '+00:00'
dt.TimeZone = 'America/New_York'
dt = datetime
27-Dec-2022 09:55:52
dt.TimeZone = 'local'
dt = datetime
27-Dec-2022 14:55:52
Hi Steven,
Thank you for the code but I got thi error:
Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To construct matrices, use brackets instead of parentheses.
Would you please tell me how to resolve it?

Sign in to comment.

 Accepted Answer

Jan
Jan on 26 Dec 2022
Edited: Jan on 26 Dec 2022
The command tzoffset accepts one input only:
[dt,dst] = tzoffset(t)
Therefore the additional arguments 'EST','5' are not valid.
Maybe you mean
T = datetime(selectedData(:,3)./3600, ...
'ConvertFrom', 'epochtime', 'Epoch', '1970-01-01', 'TimeZone', 'UTC');
T.TimeZone = 'America/New_York'; % Set accordingly, perhaps 'system'

3 Comments

Hi Jan,
Thank you for your response.
I used the code that you mentioned above. The result is like this, I want time from 0-24 but the time that gave me is for 20 min. Please see the figure attached.
That looks like a problem with your conversion from epoch time, but without knowing the values of selectedData, it's hard to know what the appropriate conversion factor should be.
Hi Eric,
Thank you for your comment.
Here is the selected data:
select = ~isMasked & selectElevation & selectPRN ;
selectedData = gpsData{stationId}.data(select,:) ;

Sign in to comment.

More Answers (0)

Categories

Asked:

Ara
on 26 Dec 2022

Commented:

Ara
on 28 Dec 2022

Community Treasure Hunt

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

Start Hunting!