How to get UTC time in my program?

Dear All,
I am wondering if it is pissible to get the UTC time in my program. Do I need the internet avaibale if I want to get UTC time?
Thanks a lot.
Benson

 Accepted Answer

Try this:
dtLCL = datetime('now', 'TimeZone','local') % Current Local Time
dtUTC = datetime(dtLCL, 'TimeZone','Z') % Current UTC Time
That will give you both the local time and the corresponding UTC time.
.

5 Comments

Hi, Star,
Thanks for your answer.
My computer time is Not accurate, but I want to obtain the accurate UTC time. How can I do it?
Thanks.
Bensen
My pleasure.
I have not done this for many years (likely since the early 1990s). However NIST Internet time service , NIST Internet time servers, and NIST TIme Widget are three options.
Back in the 1980s and early 1990s, I had my computer (MS-DOS, early Windows) access those and set my computer clock with them every time I booted it. I wrote my own FORTRAN code to do that (long before I was introduced to MATLAB.) You can probably use the ftp or webread functions (neither of which I have used in the past few years) to get the time. Setting the time may be more of a problem, since I never had to do that in Windows 10 or recent earlier Windows versions. That might require an operating system call.
That is the best I can do.
Hi, Star,
Thanks a lot for your great help.
Benson
As always, my pleasure!
This gives you a simple network time, which does not rely on local computer time:
url_time = webread('http://worldtimeapi.org/api/timezone/Etc/UTC')

Sign in to comment.

More Answers (1)

utc_time = datetime('now', 'timezone', 'utc')

1 Comment

This just converts your local computer time to UTC. So if your local computer time is incorrect for any reason, the utc_time will be also incorrect.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!