Convert time vector of Year, Month, Day, Hours, Minute to Decimal format
15 次查看(过去 30 天)
显示 更早的评论
Hi everyone;
Suppose I have the following time vector:
....start from January
2019 10 02 12:00:00
2019 10 03 12:00:00
2019 10 04 12:00:00
2019 10 05 12:00:00
2019 10 06 12:00:00
2019 10 07 12:00:00
2019 10 08 12:00:00
2019 10 09 12:00:00
2019 10 10 12:00:00
2019 10 11 12:00:00
2019 10 12 12:00:00
2019 10 13 12:00:00
.....
I want to convert this time vector to decimal format, so it become:
2019,xxxx
2019,xxxx
2019,xxxx
etc
Any idea to do it, really appreciate it.
1 个评论
Siddharth Bhutiya
2021-12-30
May I ask what you are planning to do with the (2019.xxxx) once you have calculated that? Because when you have a datetime object, it knows what operations can and cannot be done on a datetime. When you convert that into a double 2019.xxxx, that knowledge goes away. This could cause unexpected result if you are not careful later on in your code. For example, if you accidentally add 1 to it it changes the year from 2019 to 2020. You could multiply it with anything because its a double now, however, multiplication would not make sense for datetimes. So it would be helpful to know what you intend to do with those values.
采纳的回答
Stephen23
2021-12-18
编辑:Stephen23
2021-12-18
Simpler:
format long G
D = datetime(2019,10,(2:13).',12,0,0)
B = dateshift(D, 'start', 'year'); % midnight at start of the year
E = B + calyears(1); % midnight at the end of the year (do not use DATESHIFT)
Y = year(D);
Y = Y + (D-B)./(E-B)
Lets check the first fraction by hand:
between(B(1),D(1),'Time') % hours from start of year to midday 2nd Oct.
between(B(1),E(1),'Time') % total hours in the year
6588/8760
2 个评论
Stephen23
2021-12-18
编辑:Stephen23
2021-12-18
Another approach is to download DATESTR8601 (its output also confirms the values shown in my answer):
format long G
D = datetime(2019,10,(2:13).',12,0,0);
S = arrayfun(@(d)datestr8601(d,'y11'),D,'uni',0)
Note that it returns character vectors, not numerics.
datestr8601([2020,12,31,12,0,0],'y11')
datestr8601([2021,1,1,6,11,34],'y11')
Note that this function is now quite old and relies internally on datevectors and datenumbers, so is less precise than calculating with datetime objects.
更多回答(2 个)
Steven Lord
2021-12-17
% Sample data
d = datetime(2019, 10, (2:6).', 12, 0, 0)
startOfYear = dateshift(d, 'start', 'year')
p = years(d-startOfYear)
% Use that year fraction data
corresponding = datetime(2021, 1, 1) + years(p)
2 个评论
Stephen23
2021-12-18
编辑:Stephen23
2021-12-18
format long G
365.2425 * 24 % hours
whereas in fact the length of a calendar year is a) never equal to this value and b) not constant. The bug in this approach can be clearly demonstrated at the end of a leap year, where this calculation returns midday on december the 31st as a fraction greater than one:
d = datetime(2020, 12, 31, 12, 0, 0) % leap year!
startOfYear = dateshift(d, 'start', 'year')
d-startOfYear % correct
p = years(d-startOfYear) % flawed
Adding this fraction to a year would give an ambiguous numeric value which can represent multiple dates, e.g. it would be impossible to tell the difference between 2020-12-31T12:00:00 and 2021-01-01T06:11:34.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!