【问题标题】:Convert hh:mm time to decimal time (Matlab)将 hh:mm 时间转换为十进制时间(Matlab)
【发布时间】:2020-12-18 14:41:04
【问题描述】:

我正在尝试使用 Matlab 以十进制小时数获取日出和日落时间。请看下面的代码。我已经到了结果是 2 个 1x2“单元”数组的地步,其中第一个单元中的时间表示为“hh:mm”。我想将其转换为十进制时间,例如将 '08:41' 转换为 8.68333 作为双精度数组。我该怎么做?

谢谢。

clear all;
close all;
tic;

startDate = datetime(date, 'ConvertFrom', 'yyyymmdd', ...
                     'TimeZone', 'UTC', ...
                     'Format', 'd MM yyyy');

%% Download sunset and sunrise time 

date_day = day(startDate);
date_month = month(startDate);
date_year = year(startDate);

% Brussels

fullURL1 = sprintf('http://api.aladhan.com/v1/calendarByCity?city=Brussels&country=Belgium&method=2&month=%02d&year=%04d',date_month,date_year);
api = fullURL1;
url = [api 'country'];
S = webread(url);

sunrise = sprintf('S.data(%02d).timings.Sunrise',date_day);
sunrise = eval(sunrise);
sunrise = strsplit(sunrise);
sunset = sprintf('S.data(%02d).timings.Sunset',date_day);
sunset = eval(sunset);
sunset = strsplit(sunset);

clearvars url fullURL1 api S

【问题讨论】:

    标签: matlab datetime time


    【解决方案1】:

    您可以转换为 datenum,从中取日分数并乘以 24。例如:

    f = rem(datenum('08:41', 'HH:MM'), 1)*24 % format is optional
    %f =
    %    8.6833
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-09
      • 2021-12-24
      • 2017-04-18
      • 1970-01-01
      • 2021-11-15
      • 2021-07-22
      • 2011-07-09
      相关资源
      最近更新 更多