Calculate future date by days. - Printable Version
+- SA-MP Forums Archive (
https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (
https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (
https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Calculate future date by days. (
/showthread.php?tid=344804)
Calculate future date by days. -
Roel - 22.05.2012
Hello,
My question is does somebody have a function that calculates a future date, exampe;
Today - 22-05-2012
Days - 20
New date (by function) - 11-06-2012
I created this:
pawn Код:
new Year, Month, Day, MaxDays,expirestamp;
getdate(Year, Month, Day);
expirestamp = gettime() + 92 * 1 * 24 * 60 * 60 * 1000;
Month += 3;
switch(Month)
{
case 1: MaxDays = 31;
case 2: MaxDays = 28;
case 3: MaxDays = 31;
case 4: MaxDays = 30;
case 5: MaxDays = 31;
case 6: MaxDays = 30;
case 7: MaxDays = 31;
case 8: MaxDays = 31;
case 9: MaxDays = 30;
case 10: MaxDays = 31;
case 11: MaxDays = 30;
case 12: MaxDays = 31;
}
if(Day > MaxDays)
{
Day -= MaxDays;
Month++;
if(Month > 12)
{
Month = 1;
Year++;
}
}
format(newdate,26,"%02d/%02d/%d",Day,Month,Year);
But it fails when you add more then 31 days.
It only works for 1 month into the future.
If I want to edit it for more it will be alot of code, so I hope somebody already got a faster and smaller function for this?
[EDIT] a function to convert unix timestamp to a date string is also good!!
Re: Calculate future date by days. -
Roel - 22.05.2012
I already sloved it:
pawn Код:
for(new i = 0; i < 92; i++) // 92 days into the future.
{
Day++;
switch(Month)
{
case 1: MaxDays = 31;
case 2: MaxDays = 28;
case 3: MaxDays = 31;
case 4: MaxDays = 30;
case 5: MaxDays = 31;
case 6: MaxDays = 30;
case 7: MaxDays = 31;
case 8: MaxDays = 31;
case 9: MaxDays = 30;
case 10: MaxDays = 31;
case 11: MaxDays = 30;
case 12: MaxDays = 31;
}
if(Day > MaxDays)
{
Day -= MaxDays;
Month++;
if(Month > 12)
{
Month = 1;
Year++;
}
}
}