Data360 Analyze

 View Only
  • 1.  adding months to a given date

    Employee
    Posted 10-14-2019 03:46

    I tried to use addDate function as given in Data3Sixty Analyze Help Documentation. But it is not working and throws Attribute error. I have tried to use this function with datetime.date object. Please let me know how can I add certain number of years, months or days to a particular date.



  • 2.  RE: adding months to a given date

    Employee
    Posted 10-14-2019 04:47

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    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



  • 3.  RE: adding months to a given date

    Employee
    Posted 10-14-2019 06:56

    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.



  • 4.  RE: adding months to a given date

    Employee
    Posted 10-14-2019 07:37

    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

     



  • 5.  RE: adding months to a given date

    Employee
    Posted 10-14-2019 07:41

    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



  • 6.  RE: adding months to a given date

    Employee
    Posted 10-14-2019 07:58

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    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)


  • 7.  RE: adding months to a given date

    Employee
    Posted 10-14-2019 08:17

    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