필터 지우기
필터 지우기

How to display a Localized Time of a specific city?

조회 수: 1 (최근 30일)
Mohamed Habiballa Abdelmoez
Mohamed Habiballa Abdelmoez 2018년 8월 21일
Hi, I am working on a program to calculate the Time Difference between cities. A part of the program is to display the current Date/Time of each City.
My question is How do I get the current time of a specific city?
I tried to use:
t = datestr(datetime('now','TimeZone','local','Format','dd-mm-yyyy HH:mm:ss Z')); %%get the time
t.TimeZone = 'America/NewYork' ; %%define the city
but it did not work. Even I tried to write the Time Zone as bellow:
t = datestr(datetime('now','America/NewYork','local','Format','dd-mm-yyyy HH:mm:ss Z')); %%get the time
but I am still not getting results.
Thank You,

채택된 답변

Star Strider
Star Strider 2018년 8월 21일
You need to use the correct format, and the correct syntax for the time zone. (I am using R2018a, although I would be surprised if this format and syntax have changed since it was introduced in R2014b.)
Also, you do not need the datestr call.
With those changes:
t = datetime('now','TimeZone','local','Format','dd-MM-yyyy HH:mm:ss Z'); %%get the time
t.TimeZone = 'America/New_York' ; %%define the city
produces:
t =
datetime
21-08-2018 14:05:58 -0600
t =
datetime
21-08-2018 16:05:58 -0400
Note: I am in the U.S. Mountain time zone (equivalent to 'America/Denver'), thus the offset.
  댓글 수: 4
Star Strider
Star Strider 2018년 8월 23일
@Steven — Thank you.
Mohamed Habiballa Abdelmoez
Mohamed Habiballa Abdelmoez 2018년 8월 25일
편집: Mohamed Habiballa Abdelmoez 2018년 8월 25일
Thank you EVRYONE, I finally found out the problem: As @Steven Lord said I cant set 'char' to be displayed as 'string', so I just needed to convert the formatted 'datetime' into formatted string as follow:
t = datestr(datetime('now','TimeZone','local',...
'Format','dd-mm-yyyy HH:mm:ss Z'));
%%get the time
t.TimeZone = 'America/NewYork' ; %%define the city
datestr(t); %%convert 't' to formatted string
set(handles.text1, 'string' , t); %%display on textbox

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Peter Perkins
Peter Perkins 2018년 8월 24일
Also, the timezones function will pop up a browser window with all your choices for time zones. It's kind of interesting reading.

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by