Date Problem (+rep)
#11

Quote:
Originally Posted by AnthonyTimmers
Посмотреть сообщение
That still will result in dates such as 35/05/2014. You'll need to calculate if it's the next month. Which would require an array with the length of months and also some way to deal with leap years.
That was my first thought too, but then I came up with this:
pawn Код:
new year, month, day, maxdays;
    getdate(year, month, day);
    printf("Date: %02d/%02d/%d", day, month, year);
    switch(month)
    {
        case 1, 3, 5, 7, 8, 10, 12: maxdays = 31;
        case 4, 6, 9, 11: maxdays = 30;
        case 2: maxdays = ((!(year % 4)) ? (29) : (28));
    }
    if((day += 5) > maxdays) day -= maxdays, month++;
    if(month > 12) month -= 12, year++;
    printf("Date +5 days: %02d/%02d/%d", day, month, year);
Simply replace 'year', 'month' and 'day' with your own variables.

Код:
case 1, 3, 5, 7, 8, 10, 12: maxdays = 31;
		case 4, 6, 9, 11: maxdays = 30;
		case 2: maxdays = ((!(year % 4)) ? (29) : (28));
This says, if month is 1, 3, 5, 7 etc., the maximum days in that month will be 31. This never changes depending on the year.
If the month is 4, 6, 9, 11, the maximum days in that month will be 30. This never changes depending on the year.
If the month is February, we need to check if it's a leap year to determine whether the maximum days will be 28 or 29.
Код:
maxdays = ((!(year % 4)) ? (29) : (28));
Leap years occur every 4 years, so we can simply just see whether the year is divisible by 4 by doing: '!(year % 4)'. Thus we can determine whether it is a leap year or not. This pretty much translates to:
pawn Код:
if(leapyear) maxdays = 29;
else maxdays = 28;
Код:
if((day += 5) > maxdays) day -= maxdays, month++;
If adding 5 days to the number of days is greater than the maximum days in that month, we need to change the month and reset the days.
Код:
if(month > 12) month -= 12, year++;
If we happen to be in December and moving into the next year, this updates the years and resets the months.
Reply


Messages In This Thread
Date Problem (+rep) - by ScripteRMKD - 30.11.2014, 18:06
Re: Date Problem (+rep) - by UltraScripter - 30.11.2014, 18:09
Re: Date Problem (+rep) - by ScripteRMKD - 30.11.2014, 18:13
Re: Date Problem (+rep) - by UltraScripter - 30.11.2014, 18:17
Re: Date Problem (+rep) - by CutX - 30.11.2014, 18:27
Re: Date Problem (+rep) - by ScripteRMKD - 30.11.2014, 18:37
Re: Date Problem (+rep) - by AnthonyTimmers - 30.11.2014, 21:03
Re: Date Problem (+rep) - by ScripteRMKD - 30.11.2014, 21:36
Re: Date Problem (+rep) - by AnthonyTimmers - 30.11.2014, 21:45
Re: Date Problem (+rep) - by ScripteRMKD - 30.11.2014, 23:16
Re: Date Problem (+rep) - by Threshold - 01.12.2014, 01:53
Re: Date Problem (+rep) - by ScripteRMKD - 01.12.2014, 11:24
Re: Date Problem (+rep) - by ScripteRMKD - 01.12.2014, 11:38
Re: Date Problem (+rep) - by Threshold - 01.12.2014, 11:53
Re: Date Problem (+rep) - by ScripteRMKD - 01.12.2014, 13:31
Re: Date Problem (+rep) - by Threshold - 01.12.2014, 13:52
Re: Date Problem (+rep) - by ScripteRMKD - 01.12.2014, 14:13
Re: Date Problem (+rep) - by ScripteRMKD - 01.12.2014, 17:37
Re: Date Problem (+rep) - by Threshold - 01.12.2014, 22:26
Re: Date Problem (+rep) - by AnthonyTimmers - 01.12.2014, 23:01

Forum Jump:


Users browsing this thread: 1 Guest(s)