create function that takes a person's birthday in year-month-day format and calculates the number of days until their next birthday from the current date. in matlab

main.m
function daysUntilNextBirthday = calculateDaysUntilNextBirthday(birthday)
    % Convert the input string to a datetime object
    birthdayDate = datetime(birthday, 'InputFormat', 'yyyy-MM-dd');
    
    % Get the current date
    currentDate = datetime('today');
    
    % Calculate the next birthday
    nextBirthday = datetime(currentDate.Year, birthdayDate.Month, birthdayDate.Day);
    
    % If the next birthday has passed for this year, increase the year by 1
    if nextBirthday < currentDate
        nextBirthday.Year = nextBirthday.Year + 1;
    end
    
    % Calculate the number of days until the next birthday
    daysUntilNextBirthday = daysact(currentDate, nextBirthday);
end
693 chars
19 lines

related categories

gistlibby LogSnag