在matlab中显示十进制时间的时钟时间

时间:2013-12-09 02:54:43

标签: matlab time decimal clock

我将此代码作为计算太阳时间的程序的一部分。但我的计算使用小数时间。现在,我需要在时钟时间显示结果。

%To calculate solar time
stime = time + (((4*(Lloc-Lsm))+ Et)/60);
disp(['The true solar time is ' num2str(stime),'hr']); %To display the solar time

答案是这样的:

The true solar time is 13.0501hr

如何将时间转换为时钟时间(12小时或24小时格式)。即分钟应乘以60(0.0501 * 60min = 3.006),时间应显示为13:03或1:03 PM。

感谢您的帮助。谢谢。 此致,-researcher -

2 个答案:

答案 0 :(得分:2)

您可以使用以下

my_hour = floor(stime);
my_minute = round(mod(stime, 1) * 60);

disp(['The true solar time is ', num2str(my_hour), ':', num2str(my_minute)]);

答案 1 :(得分:1)

使用datenum转换为序列日期编号,然后datestr使用适当的格式构建字符串:

h = 13.0501; %// your computed decimal time
string = datestr(datenum([0 0 0 h 0 0]),14); %// or change "14" for other formats

您指定的两种格式对应datestr中的格式13到16(请参阅上面文档的链接)。例如,对于格式14,字符串是

>> disp(string)
 1:03:00 PM
相关问题