Jump to content

need advice


TheDarkhand
 Share

You are about to reply to a thread that has been inactive for 1705 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

I have looked threw many time/ date scripts but having a hard time making a small system that will take the current date and add 9 months or 3 months. I keep having issues when it crosses the year and the script adding the correct year. Example if the current date was 2019-08-22 and I added 9 months the date keeps returning 2019-05-22 not 2020-05-22. any advice on this.

Link to comment
Share on other sites

is not clear from the text what you want to happen when a day is greater than 28.  I.e. 31 December + 2 months is 31 February.  If we assume that day will not be greater than 28 then a way it can be coded is. Example:

// assumes 
// 1) date is a string formatted the same as llGetDate() YYYY-MM-DD
// 2) day of month is in 1..28. When day is in 29..31 then potential incorrect newdate

default
{
    state_entry()
    {
                
        string date = "2019-06-01";
        
        integer addmonths = 10;
            
        integer year = (integer)date;
        integer month = (integer)llGetSubString(date, 5, 6) + addmonths;
        if (month > 12)
        { 
            month = month % 12;
            year++;
        }
        
        string newdate = (string)year + "-"
            + llGetSubString("0" + (string)month, -2, -1) // pad leading 0
            + llGetSubString(date, -3, -1);  // same day 
        
        llOwnerSay(date + " : " + newdate);
     }
}

if the rule for handling potential incorrect 29..31 was to  limit these to the true last day of a month then is doable but requires a bit more work. I.e 31 December + 2 months is 28 February or 29 February in a leap year

a issue with doing this is: 31 December + 2 months = 28 February + 2 months = ?what? April.  28 April, 29 April or 30 April ?

  • Thanks 1
Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 1705 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...