How to use DATE Excel Function in Matlab vector calculations?
2 views (last 30 days)
Show older comments
Hi all. New around here. I have been using excel date function con get dates for a insurance policy. This is my formula and would like it to translate cleanly to matlab syntax.
Excel formula: =MIN(E;MAX(B;DATE(YEAR(T)-IF(MONTH(B)<MONTH(T)+1;0;1);MONTH(B);DAY(B))))
Where:
E: Expiring date.
B: Begining date.
T: Today
I would like to apply the code to a complete table. I´ve been thinking on doing it in separate columns, but I am sure there a more efficient approach...
Thanks!
Edson.
0 Comments
Accepted Answer
Steven Lord
on 2 May 2021
Don't work with serial date numbers unless you absolutely have to. Use a datetime array instead.
dt = datetime('now') + hours(randi([-5, 5], 1, 3))
min(dt)
minutes(dt(3)-dt(1))
2 Comments
Steven Lord
on 4 May 2021
There's no need to use serial date numbers.
inputFormat = 'ddMMyyyy'; % For datetime mm is minute and MM is month
T = datetime('31122016', 'InputFormat', inputFormat)
B = datetime('01102016', 'InputFormat', inputFormat)
E = datetime('30102020', 'InputFormat', inputFormat)
There are functions for extracting the parts of a datetime array:
[Y, M, D] = ymd(T)
You can use those parts in reassembling a datetime. Or you could just perform date arithmetic using a datetime and either a duration or calendarDuration array.
Tplus1_version1 = datetime(Y, M+1, D)
Tplus1_version2 = T + calmonths(1)
You can do some other calculations.
[value, location] = min([T, B, E])
Since location was 2, the second element of the datetime vector [T, B, E] contains the minimum.
More Answers (0)
See Also
Categories
Find more on Dates and Time in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!