We recommend switching to the latest versions of Edge, Firefox, Chrome or Safari. Using Internet Explorer will result in a loss of website functionality.

adding months to a given date

Comments

6 comments

  • Avatar
    Gerry Mullin

    You can use timedelta in a Transform node and add 30 days to the date. 

    https://docs.python.org/2/library/datetime.html#datetime.timedelta

    0
    Comment actions Permalink
  • Avatar
    Sumeet Soni

    Hey, I even tried timedelta. It still throws attribute error. Can you please give me an example please?

    May be, it doesn't work with the syntax explained in link.

     

    Also, timedelta doesn't work for months. I can not add 30 * number of months as different months have different number of days. Either I will have to calculate number of days manually and add it to get the exact result.

    0
    Comment actions Permalink
  • Avatar
    Adrian Williams

    As Gerry states, the logic for adjusting a datetime.date value by a specified interval uses the Python timedelta object. The basic logic required is as follows (see attached example data flow):

     

     

    The addDate function relates to the proprietary script that is used with the superseded nodes of the legacy Lavastorm product.

    As indicated in the Python documentation, timedelta objects support intervals of fractional seconds, seconds, minutes, hours, days and weeks.

     

    Attached files

    Add_Day_to_Date_Value - 14 Oct 2019.lna

     

    0
    Comment actions Permalink
  • Avatar
    Adrian Williams

    You may want to consider using the date.replace() function to modify a date value by a specified number of months as described in the documentation on date objects:

     https://docs.python.org/2/library/datetime.html#date-objects

    0
    Comment actions Permalink
  • Avatar
    Gerry Mullin

    I put this in a Calculate Fields node (connected to a Create Data node). You can use this in a Transform also. Note you'll have to explicitly handle Null values.

    dueDate + datetime.timedelta(days=30)

    You're right that it does not take months. When you say add a month do you mean just move the month up one, i.e. today's value would become 2019-11-14?

    If so you could do some if statement, or maybe a dict to store how many days you go forward based on the month You could also calculate the month (dueDate.month) value and add one. Here's an example of one way to go about it. This is not handling any Null dates date that could be in the data.

    dueMonth = in1.dueDate.month
    dueYear = in1.dueDate.year


    if dueMonth == 9 or dueMonth == 4 or dueMonth == 6 or dueMonth == 11: # 30 day months
    days = 30
    elif dueMonth == 2 and dueYear % 4 == 0: # leap year
    days = 29
    elif dueMonth == 2:
    days = 28
    else:
    days = 31

    out1.NewDate = in1.dueDate + datetime.timedelta(days=days)
    0
    Comment actions Permalink
  • Avatar
    Adrian Williams

    Here is a basic example of the use of the date.replace() function.

    Note: the example assumes the number of months n is between 1 and 12. Supplemental logic would be required to handle an arbitrary number of months.

     

    Attached files

    Add_Day_or_n_Months_to_Date_Value - 14 Oct 2019.lna

     

    0
    Comment actions Permalink

Please sign in to leave a comment.