Search code examples
matlabmatlab-figure

How can I make an algorithm that can give me the right values for year, month,day ,h,min,s?


I'm trying to make an algorithm that takes count if a year is a leap year and also to make it recognize the number of days of every month so that we have for example 31 december 2001 and the time is 23:59:00 and we add a minute we will obtain 1 jan 2002 00:00:00 . But the script seems to be faulty and the results aren't right.This is an algorithm for a function but I also want to put it in guide where a user could write the date using an editbox. The problem in this situation is what happens if a user puts as day 44 for example, The algorithm only takes account if the day is greater than 30, 31 ,28,29 and makes the day 1. Also another problem is if a user puts hour =0 , in my function I have that hour is 0 , day=day+1.THis condition was put because I have a tspan and the algorithm should work based the transformation of second in min,h,day.. Can somebody offer me some guidance over this problems that I'm trying to avoid? For the problem with the user input I was thinking of a notice message or something that won't let the user put a bigger value for that month.

 t=3600;
year=2001;
anb=0; %leap year
%condition for leap year
 if mod(year,4)~=0
        anb=0;
    else if mod(anb,100)~=0
            anb=1;
        else if mod(anb,400)~=0
                anb=0;
            else anb=1;
            end
        end
 end
month= 12;
day=28;
hour=23;
min=25;
sec=17;
sec = sec + rem(t, 60);
min = rem(min + floor(t/60), 60);
hour = rem(hour + floor(t/3600), 24);
%conditions
if hour==0
    day=day+1;
end

 if (anb==1 && month==2 && day >29)
                month=3;
                day=1;

            else if anb==0 && month==2 && day>28
                    month=3;
                    day=1;


                end
 end
if month==1||3||5||7||8||10 && day>31
    month=month+1
    day=1
else if month==4||6||9||11 && day>30
        month=month+1
        day=1
    else if month==12 && day>31
            year=year+1
            month=1
            day=1
        end
    end
end

Solution

  • It's not worth it to write this yourself. Dealing with the leap seconds, leap years, and all that is quite a pain. It sounds like datenum and datevec can meet your needs. One thing to note is that datenum uses days as units which explains the use of secPerDay below.


    Adding time to a date

    secPerDay = 60*60*24;
    
    Y = 2001;
    M = 12;
    D = 31;
    H = 23;
    MN = 59;
    S = 0.0;
    n = datenum(Y,M,D,H,MN,S);
    
    % add 60 seconds to the date
    n1 = n + 60 / secPerDay;
    
    % convert back to year, mon, day, hour, minute, second
    [Y1,M1,D1,H1,MN1,S1] = datevec(n1);
    

    Results

    Y1 = 2002
    M1 = 1
    D1 = 1
    H1 = 0
    MN1 = 0
    S1 = 0
    

    Improper dates

    Also note that datenum deals with improper inputs in an intuitive way (at least it appears to in MATLAB2016b).

    secPerDay = 60*60*24;
    
    Y = 2001;
    M = 12;
    D = 42;  % Ah yes, I have fond memories of December 42nd, 2001...
    H = 23;
    MN = 59;
    S = 0.0;
    % fix the date using datenum & datevec
    n = datenum(Y,M,D,H,MN,S);
    [Y,M,D,H,MN,S] = datevec(n);
    

    Results

    Y = 2002
    M = 1
    D = 11
    H = 23
    MN = 59
    S = 0
    

    Time between two dates

    If you want the number of seconds between two dates just subtract the datenums.

    secPerDay = 60*60*24;
    dt = (datenum(2002,1,1,0,0,0) - datenum(2001,12,31,23,59,0)) * secPerDay
    

    Results

    dt = 60.0000