Skip to content

DateTime: Support adding months and yeards to dates #4890

Description

@kchaudhari0

Hi,
In Robot currently, we don't have the option to add months to the current data in the DateTime library.
We can now add Hours, Days, and, Years using Add Time to data.

${one_month_later} Add Time To Date ${current_date} 1 months

it we try to add the above it will not recognize as a keyword.

Activity

  1. changed the title [-]Add option to add months in Current date[/-] [+]Feature Request :Add option to add months in Current date[/+] on Oct 7, 2023
  2. pekkaklarck commented on Oct 25, 2023

    @pekkaklarck
    Member

    Month is problematic because it can e 28-31 days. The code for handling formats like "1 day 2 hours" is generic and cannot really handle months. In this particular context incrementing and decrementing months could probably be handled separately, though. I personally don't consider this too high priority, but if someone is interested to implement this I'm happy to review a pull request and discuss about some of the corner cases dealing with months would contain.

  3. kadsat commented on Mar 7, 2025

    @kadsat

    @pekkaklarck , Is this because timedelta object doesn't support months? can I give this a try?

  4. pekkaklarck commented on Mar 12, 2025

    @pekkaklarck
    Member

    The problem is that our custom time string format that can be used like "1 day 2 hours" doesn't support months. This format is internally converted to seconds (as a float) that can then be converted to a timedelta object.

    Adding support for months is problematic, because the exact amount depends on the context. I think it's pretty much impossible to add months support to our time string format.

    It would probably be possible to support months with rhis particular keyword, though. It would require something like this:

    • The keyword needed to have a custom support for months. Possibly it could separately recognize values like "1 month" and "4 months". Something like "1 month 1 day" would be harder to handle.
    • Incrementing by months could be handled by changing the month attribute of the underlying datetime object. How to handle cases like adding one month to 2025-01-31 would need to be solved, though.

    All in all, I think there could be too many corner cases and too many design decisions to be made for this to be worth implementing. If you @kadsat are interested you certainly can try, but there should be more relevant and more straightforward issues available as well.

  5. kadsat commented on Apr 15, 2025

    @kadsat

    @pekkaklarck , hey sorry I couldn't respond earlier. I was on vacation travel, I am thinking of something along these lines, please let me know your opinion...

    seeking guidance to see if I am on the right track..

    def add_month_to_a_date(
            date : datetime.datetime, 
            months: int
        ) -> datetime.datetime:
        '''
        need to implement negative months
        '''
        day   = date.day
        month = date.month
        year  = date.year
    
        rel_year , rel_month = int(months/12), int(math.remainder(months,12))
        _, max_days = calendar.monthrange(year+rel_year,month+rel_month)
        rel_day = min(
            day, 
            max_days
        )
        return datetime.datetime(
                month= month +rel_month,
                day= rel_day,
                year= year + rel_year
            )
  6. pekkaklarck commented on Apr 16, 2025

    @pekkaklarck
    Member

    Can you give examples with different inputs?

  7. kadsat commented on Apr 19, 2025

    @kadsat
    Input Output
    date - 1/31/2025 , months = 1 2/28/2025
    date - 1/15/2025 , months = 1 2/15/2025
    date - 2/28/2025, months = 1 3/28/2025
    date - 4/30/2025, months = 2 6/30/2025
    date - 1/31/2025, months = 3 4/30/2025

    please let me know if there are any additional edge cases to consider. Note, I am only considering addition.. Subtraction would need additional logic..

  8. pekkaklarck commented on Apr 19, 2025

    @pekkaklarck
    Member

    The basic idea is to just increment/decrement the month and leave the day part to the original value, right? The day would need to be scaled down if it would be over the maximum of the month, though, and the year would also need to be updated if going over the boundary one way or the other. I guess that makes sense. Could you anyway ask opinions from others e.g. on the #robotframework channel on our Slack? You can point them to this issue.

  9. kadsat commented on Apr 19, 2025

    @kadsat

    @pekkaklarck , yes you're correct. I am aiming to just change the month and leave the dates intact.If it falls out of range then I try to bring it down to the most reasonable value.

    Certainly I will post on the channel and solicit feedback.

  10. added this to the v7.5 milestone on Aug 3, 2026
  11. pekkaklarck commented on Aug 3, 2026

    @pekkaklarck
    Member

    PR #5729 looks so good that I added this to RF 7.5 scope.

  12. pekkaklarck commented on Aug 3, 2026

    @pekkaklarck
    Member

    There are some design decisions related to how incrementing or decrementing months is handled that I want to explicitly document:

    1. The day part is generally not changed. If the day is not valid to the resulting month, it is scaled down to the last valid day, though. For example, adding a month to Match 31 yields April 30.
    2. Year is incremented (or decremented) as needed.
    3. The time part is never touched.
  13. pekkaklarck commented on Aug 3, 2026

    @pekkaklarck
    Member

    PR #5729 adds separate keywords for incrementing and decrementing months. We could still decide to change that.

    First of all, we could just add one keyword for adding months that works also with negative values. The added keywords already handle negative values, so this would only require removing the keyword for decrementing months. We have separate keywords for adding and removing other time intervals, so for symmetry having two keywords also for this usage is probably better.

    Yet another alternative would be adding months support the current Add Time To Date and Subtract Time From Date keywords. As I have commented above, adding months support to the time string format is not really possible, though, because those time strings are converted to seconds and we cannot map months to fixed seconds. We could, however, change these keywords so that they handle months given like 1 month or 6 months differently than normal time strings. That would avoid the need to add these special keywords for months altogether. I kind of like that, but I'm not entirely sure is that better or not. In practice we needed to decide do we prefer

    ${new} =    Add Months To Date    ${date}    1
    

    or

    ${new} =    Add Time To Date    ${date}    1 month
    
  14. pekkaklarck commented on Aug 4, 2026

    @pekkaklarck
    Member

    PR #5729 has been closed for some reason. If that wasn't an accident and someone else isn't interested to look at this, I'll move this issue away from RF 7.5 scope.

  15. pekkaklarck commented on Aug 4, 2026

    @pekkaklarck
    Member

    After thinking this enhancement a bit more, I believe it's better to add months support to Add Time To Data and Subtract Time From Date keywords. If we do that, we could also add support to add years using the same approach instead of adding new keywords for that purpose as well.

    There are two ways how to do that:

    1. Enhance Add Time To Data and Subtract Time From Date keywords so that they recognize inputs like 1 month and 2 years and handle them separately. The internal Data class could then get methods add_months and add_years similarly as in the now closed PR #5729. This would have a problem that usages like 1 year 1 day wouldn't work. I guess that's better than not supporting months and years at all, but the limitation would still be annoying.

    2. Enhance timestring_to_secs so that it handles years and months. I've earlier commented that it isn't possible because months (and years) don't have a fixed length. That's true in general, but if we know the start date, then handling months and years is actually possible. We could thus enhance timestring_to_secs so that it optionally accepts the start date and when it's given, handles also years and months. This is more complicated than just enhancing Add Time To Data and Subtract Time From Date, but it would also make it easy to add year and month support to other keywords and other usages in general.

    I believe the second option is much better. It avoids the need to explain why you can use 1 month but not 1 month 2 days, and it also makes it easy to support months and years also elsewhere. If someone needs months and years support urgently, I'd be fine with using the option 1 in RF 7.5 and switching to option 2 later, though.

  16. changed the title [-]Feature Request :Add option to add months in Current date[/-] [+]DateTime: Support adding months and yeards to dates[/+] on Aug 4, 2026
  17. modified the milestones: v7.5, v7.6 on Aug 10, 2026
  18. fzlzjerry commented on Aug 10, 2026

    @fzlzjerry

    I'd like to take this over now that #5729 is closed, following the second
    direction from the latest discussion.

    I'll extend timestr_to_secs() with an optional start date so integer calendar
    years and months can be combined with the existing fixed units. Then I'll wire
    that through the existing Add Time To Date and Subtract Time From Date
    keywords. The subtraction path will apply calendar components backwards from
    the input date rather than simply negating a forward duration, which matters at
    month boundaries such as March 31.

    The regression coverage will include end-of-month clamping, leap years,
    cross-year changes, negative values, mixed calendar/fixed units, and unchanged
    behavior when no start date is supplied. I'll work from current master and
    keep the change scoped to #4890.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions