# Convert time vector of Year, Month, Day, Hours, Minute to Decimal format

19 Ansichten (letzte 30 Tage)
Marzuki Marzuki am 17 Dez. 2021
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 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
Siddharth Bhutiya am 30 Dez. 2021
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.

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Stephen23 am 18 Dez. 2021
Bearbeitet: Stephen23 am 18 Dez. 2021
Simpler:
format long G
D = datetime(2019,10,(2:13).',12,0,0)
D = 12×1 datetime array
02-Oct-2019 12:00:00 03-Oct-2019 12:00:00 04-Oct-2019 12:00:00 05-Oct-2019 12:00:00 06-Oct-2019 12:00:00 07-Oct-2019 12:00:00 08-Oct-2019 12:00:00 09-Oct-2019 12:00:00 10-Oct-2019 12:00:00 11-Oct-2019 12:00:00 12-Oct-2019 12:00:00 13-Oct-2019 12:00:00
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)
Y = 12×1
1.0e+00 * 2019.75205479452 2019.75479452055 2019.75753424658 2019.7602739726 2019.76301369863 2019.76575342466 2019.76849315068 2019.77123287671 2019.77397260274 2019.77671232877
Lets check the first fraction by hand:
between(B(1),D(1),'Time') % hours from start of year to midday 2nd Oct.
ans = calendarDuration
6588h 0m 0s
between(B(1),E(1),'Time') % total hours in the year
ans = calendarDuration
8760h 0m 0s
6588/8760
ans =
0.752054794520548
##### 2 KommentareKeine anzeigenKeine ausblenden
Stephen23 am 18 Dez. 2021
Bearbeitet: Stephen23 am 18 Dez. 2021
format long G
D = datetime(2019,10,(2:13).',12,0,0);
S = arrayfun(@(d)datestr8601(d,'y11'),D,'uni',0)
S = 12×1 cell array
{'2019.75205479452'} {'2019.75479452054'} {'2019.75753424657'} {'2019.76027397260'} {'2019.76301369863'} {'2019.76575342465'} {'2019.76849315068'} {'2019.77123287671'} {'2019.77397260274'} {'2019.77671232876'} {'2019.77945205479'} {'2019.78219178082'}
Note that it returns character vectors, not numerics.
PS: in case anyone was curious, the dates I gave in my example here are actually:
datestr8601([2020,12,31,12,0,0],'y11')
ans = '2020.99863387978'
datestr8601([2021,1,1,6,11,34],'y11')
ans = '2021.00070693810'
Note that this function is now quite old and relies internally on datevectors and datenumbers, so is less precise than calculating with datetime objects.
Marzuki Marzuki am 25 Dez. 2021
Thank you @Stephen it works

Melden Sie sich an, um zu kommentieren.

### Weitere Antworten (2)

madhan ravi am 17 Dez. 2021
doc ymd
doc hms
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

Steven Lord am 17 Dez. 2021
% Sample data
d = datetime(2019, 10, (2:6).', 12, 0, 0)
d = 5×1 datetime array
02-Oct-2019 12:00:00 03-Oct-2019 12:00:00 04-Oct-2019 12:00:00 05-Oct-2019 12:00:00 06-Oct-2019 12:00:00
startOfYear = dateshift(d, 'start', 'year')
startOfYear = 5×1 datetime array
01-Jan-2019 01-Jan-2019 01-Jan-2019 01-Jan-2019 01-Jan-2019
p = years(d-startOfYear)
p = 5×1
0.7516 0.7543 0.7570 0.7598 0.7625
% Use that year fraction data
corresponding = datetime(2021, 1, 1) + years(p)
corresponding = 5×1 datetime array
02-Oct-2021 12:00:00 03-Oct-2021 12:00:00 04-Oct-2021 12:00:00 05-Oct-2021 12:00:00 06-Oct-2021 12:00:00
##### 2 KommentareKeine anzeigenKeine ausblenden
Marzuki Marzuki am 17 Dez. 2021
Thank you @Steven Lord but the desired result 2019.xx etc..
Stephen23 am 18 Dez. 2021
Bearbeitet: Stephen23 am 18 Dez. 2021
Note that by using YEARS this approach incorrectly assumes that a year has exactly
format long G
365.2425 * 24 % hours
ans =
8765.82
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!
d = datetime
31-Dec-2020 12:00:00
startOfYear = dateshift(d, 'start', 'year')
startOfYear = datetime
01-Jan-2020
d-startOfYear % correct
ans = duration
8772:00:00
p = years(d-startOfYear) % flawed
p =
1.0007050110543
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.

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Dates and Time finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by