I want to conver the excel dateformat('27-03-2013 06:00:00') in to matlab yearday i.e 31 jan 2013 = 31 and `1st Feb 2013 = 32

1 回表示 (過去 30 日間)
need to convert excell date ('27-03-2013 06:00:00') format into Matlab datevec or datestr and then further conver it into year day format which of the type 40.5 which means 9th Feb afternoon. where the number after the decimal refers to time.

採用された回答

Julian
Julian 2016 年 7 月 11 日
Consider:
t = {'27-03-2013 06:00:00' % in text form, apparently from Excel
'09-02-2013 12:00:00' } % an example mentioned in question
t = datetime(t, 'Inputformat', 'dd-MM-yyyy HH:mm:ss') % becomes MATLAB datetime
ival = t - datetime(year(t), 1, 1) % becomes elapsed interval since the start of the year (1st Jan same year)
ival = days(ival) % we're just interested in the number of days
result = double(ival)+1 % convert into standard number, adding 1 because 01-Jan is day 1 not day 0
Note that datetime can process numeric Excel dates directly. I inferred from your question you want the relative day number for any year, starting at day 1 on 1st Jan.
Hope this helps
Julian.
  3 件のコメント
Bikiran Das
Bikiran Das 2016 年 7 月 19 日
This code has helped me out thanks Julian
Peter Perkins
Peter Perkins 2016 年 8 月 3 日
Depending on what you want to do with "40.5", you might just leave the duration alone. Reusing Julian's example:
>> s = {'27-03-2013 06:00:00' '09-02-2013 12:00:00'};
>> t = datetime(s, 'Inputformat', 'dd-MM-yyyy HH:mm:ss');
>> dt = t - dateshift(t,'end','year','previous')
dt =
2070:00:00 972:00:00
>> dt.Format = 'd'
dt =
86.25 days 40.5 days
Or you can go all the way to numeric:
>> d = days(dt)
d =
86.25 40.5

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by