UTC to local hour in MATLAB

2019-08-31 14:15发布

问题:

I have this cell array:

    date_hour = '30/07/2012 00:00'
                '30/07/2012 01:00'
                '30/07/2012 02:00'
                '30/07/2012 03:00'
                '30/07/2012 04:00'
                '30/07/2012 05:00'
                '30/07/2012 06:00'
                '30/07/2012 07:00'
                '30/07/2012 08:00'
                '30/07/2012 09:00'
                '30/07/2012 10:00'
                '30/07/2012 11:00'
                '30/07/2012 12:00'
                '30/07/2012 13:00'
                '30/07/2012 14:00'
                '30/07/2012 15:00'
                '30/07/2012 16:00'
                '30/07/2012 17:00'
                '30/07/2012 18:00'
                '30/07/2012 19:00'
                '30/07/2012 20:00'
                '30/07/2012 22:00'
                '30/07/2012 21:00'
                '30/07/2012 23:00'

The hour is given in UTC. Once local time is -3, how can I convert to local time?

回答1:

Because the matlab date format is in days since Jan 1st, 0000 with time as a decimal, you can simply subtract 3/24 from the matlab date format.

localDateNum = datenum(date_hour,'dd/mm/yyyy HH:MM') - 3/24;

Then, you can convert it back to string if you want with datestr.

date_hour_local = datestr(localDateNum,'dd/mm/yyyy HH:MM');


回答2:

just to let you know about another solution, you can use the MATLAB addtodate-function:

date_local_num = addtodate(datenum(date_hour,'dd/mm/yyyy HH:MM'),-3,'hour');
date_local = datestr(date_local_num,'dd/mm/yyyy HH:MM');

see the documentation for more information:

http://de.mathworks.com/help/matlab/ref/addtodate.html



标签: matlab utc hour