# Date after adding given number of days to the given date

• Difficulty Level : Hard
• Last Updated : 07 May, 2021

Given a date and a positive integer x. The task is to find date after adding x days to the given date

Examples:

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

```Input : d1 = 14, m1 = 5, y1 = 2017
Days to be added x = 10
Output : d2 = 24, m2 = 5, y2 = 2017

Input : d1 = 14, m1 = 3, y1 = 2015
Days to be added x = 466
Output : d2 = 14, m2 = 3, y2 = 2016
```

1) Let given date be d1, m1 and y1. Find offset (number of days spent from beginning to given date) of given year (Refer offsetDays() below)
2) Let offset found in above step be offset1. Find result year y2 and offset of result year offset2 (Refer highlighted code below)
3) Find days and months from offset2 and y2. (Refer revoffsetDays() below).

Below is the implementation of above steps.

## C++

 `// C++ program to find date after adding``// given number of days.``#include``using` `namespace` `std;` `// Return if year is leap year or not.``bool` `isLeap(``int` `y)``{``    ``if` `(y%100 != 0 && y%4 == 0 || y %400 == 0)``        ``return` `true``;` `    ``return` `false``;``}` `// Given a date, returns number of days elapsed``// from the  beginning of the current year (1st``// jan).``int` `offsetDays(``int` `d, ``int` `m, ``int` `y)``{``    ``int` `offset = d;` `    ``switch` `(m - 1)``    ``{``    ``case` `11:``        ``offset += 30;``    ``case` `10:``        ``offset += 31;``    ``case` `9:``        ``offset += 30;``    ``case` `8:``        ``offset += 31;``    ``case` `7:``        ``offset += 31;``    ``case` `6:``        ``offset += 30;``    ``case` `5:``        ``offset += 31;``    ``case` `4:``        ``offset += 30;``    ``case` `3:``        ``offset += 31;``    ``case` `2:``        ``offset += 28;``    ``case` `1:``        ``offset += 31;``    ``}` `    ``if` `(isLeap(y) && m > 2)``        ``offset += 1;` `    ``return` `offset;``}` `// Given a year and days elapsed in it, finds``// date by storing results in d and m.``void` `revoffsetDays(``int` `offset, ``int` `y, ``int` `*d, ``int` `*m)``{``    ``int` `month = { 0, 31, 28, 31, 30, 31, 30,``                      ``31, 31, 30, 31, 30, 31 };` `    ``if` `(isLeap(y))``        ``month = 29;` `    ``int` `i;``    ``for` `(i = 1; i <= 12; i++)``    ``{``        ``if` `(offset <= month[i])``            ``break``;``        ``offset = offset - month[i];``    ``}` `    ``*d = offset;``    ``*m = i;``}` `// Add x days to the given date.``void` `addDays(``int` `d1, ``int` `m1, ``int` `y1, ``int` `x)``{``    ``int` `offset1 = offsetDays(d1, m1, y1);``    ``int` `remDays = isLeap(y1)?(366-offset1):(365-offset1);` `    ``// y2 is going to store result year and``    ``// offset2 is going to store offset days``    ``// in result year.``    ``int` `y2, offset2;``    ``if` `(x <= remDays)``    ``{``        ``y2 = y1;``        ``offset2 = offset1 + x;``    ``}` `    ``else``    ``{``        ``// x may store thousands of days.``        ``// We find correct year and offset``        ``// in the year.``        ``x -= remDays;``        ``y2 = y1 + 1;``        ``int` `y2days = isLeap(y2)?366:365;``        ``while` `(x >= y2days)``        ``{``            ``x -= y2days;``            ``y2++;``            ``y2days = isLeap(y2)?366:365;``        ``}``        ``offset2 = x;``    ``}` `    ``// Find values of day and month from``    ``// offset of result year.``    ``int` `m2, d2;``    ``revoffsetDays(offset2, y2, &d2, &m2);` `    ``cout << ``"d2 = "` `<< d2 << ``", m2 = "` `<< m2``         ``<< ``", y2 = "` `<< y2;``}` `// Driven Program``int` `main()``{``    ``int` `d = 14, m = 3, y = 2015;``    ``int` `x = 366;` `    ``addDays(d, m, y, x);` `    ``return` `0;``}`

## Java

 `// Java program to find date after adding``// given number of days.` `class` `GFG``{``    ` `// Find values of day and month from``// offset of result year.``static` `int` `m2, d2;``    ` `// Return if year is leap year or not.``static` `boolean` `isLeap(``int` `y)``{``    ``if` `(y % ``100` `!= ``0` `&& y % ``4` `== ``0` `|| y % ``400` `== ``0``)``        ``return` `true``;` `    ``return` `false``;``}` `// Given a date, returns number of days elapsed``// from the beginning of the current year (1st``// jan).``static` `int` `offsetDays(``int` `d, ``int` `m, ``int` `y)``{``    ``int` `offset = d;` `    ``if``(m - ``1` `== ``11``)``        ``offset += ``335``;``    ``if``(m - ``1` `== ``10``)``        ``offset += ``304``;``    ``if``(m - ``1` `== ``9``)``        ``offset += ``273``;``    ``if``(m - ``1` `== ``8``)``        ``offset += ``243``;``    ``if``(m - ``1` `== ``7``)``        ``offset += ``212``;``    ``if``(m - ``1` `== ``6``)``        ``offset += ``181``;``    ``if``(m - ``1` `== ``5``)``        ``offset += ``151``;``    ``if``(m - ``1` `== ``4``)``        ``offset += ``120``;``    ``if``(m - ``1` `== ``3``)``        ``offset += ``90``;``    ``if``(m - ``1` `== ``2``)``        ``offset += ``59``;``    ``if``(m - ``1` `== ``1``)``        ``offset += ``31``;` `    ``if` `(isLeap(y) && m > ``2``)``        ``offset += ``1``;` `    ``return` `offset;``}` `// Given a year and days elapsed in it, finds``// date by storing results in d and m.``static` `void` `revoffsetDays(``int` `offset, ``int` `y)``{``    ``int` `[]month={ ``0``, ``31``, ``28``, ``31``, ``30``, ``31``, ``30``,``                    ``31``, ``31``, ``30``, ``31``, ``30``, ``31` `};` `    ``if` `(isLeap(y))``        ``month[``2``] = ``29``;` `    ``int` `i;``    ``for` `(i = ``1``; i <= ``12``; i++)``    ``{``        ``if` `(offset <= month[i])``            ``break``;``        ``offset = offset - month[i];``    ``}` `    ``d2 = offset;``    ``m2 = i;``}` `// Add x days to the given date.``static` `void` `addDays(``int` `d1, ``int` `m1, ``int` `y1, ``int` `x)``{``    ``int` `offset1 = offsetDays(d1, m1, y1);``    ``int` `remDays = isLeap(y1) ? (``366` `- offset1) : (``365` `- offset1);` `    ``// y2 is going to store result year and``    ``// offset2 is going to store offset days``    ``// in result year.``    ``int` `y2, offset2 = ``0``;``    ``if` `(x <= remDays)``    ``{``        ``y2 = y1;``        ``offset2 =offset1 + x;``    ``}` `    ``else``    ``{``        ``// x may store thousands of days.``        ``// We find correct year and offset``        ``// in the year.``        ``x -= remDays;``        ``y2 = y1 + ``1``;``        ``int` `y2days = isLeap(y2) ? ``366` `: ``365``;``        ``while` `(x >= y2days)``        ``{``            ``x -= y2days;``            ``y2++;``            ``y2days = isLeap(y2) ? ``366` `: ``365``;``        ``}``        ``offset2 = x;``    ``}``    ``revoffsetDays(offset2, y2);``    ``System.out.println(``"d2 = "` `+ d2 + ``", m2 = "` `+``                            ``m2 + ``", y2 = "` `+ y2);``}` `// Driven Program``public` `static` `void` `main(String[] args)``{``    ``int` `d = ``14``, m = ``3``, y = ``2015``;``    ``int` `x = ``366``;``    ``addDays(d, m, y, x);``}``}` `// This code is contributed by mits`

## Python3

 `# Python3 program to find date after adding``# given number of days.` `# Return if year is leap year or not.``def` `isLeap(y):``    ` `    ``if` `(y ``%` `100` `!``=` `0` `and` `y ``%` `4` `=``=` `0` `or` `y ``%` `400` `=``=` `0``):``        ``return` `True``    ``return` `False` `# Given a date, returns number of days elapsed``# from the beginning of the current year (1st``# jan).``def` `offsetDays(d, m, y):``    ` `    ``offset ``=` `d``    ``switcher ``=` `{``        ``10``: ``30``,``        ``9``: ``31``,``        ``8``: ``30``,``        ``7``: ``31``,``        ``6``: ``31``,``        ``5``: ``30``,``        ``4``: ``31``,``        ``3``: ``30``,``        ``2``: ``31``,``        ``1``: ``28``,``        ``0``: ``31``    ``}``    ``if` `(isLeap(y) ``and` `m > ``1``):``        ``offset ``+``=` `1``    ``offset ``+``=``switcher.get(m)``    ``return` `offset`  `# Given a year and days elapsed in it, finds``# date by storing results in d and m.``def` `revoffsetDays(offset, y,d, m):``    ``month ``=` `[ ``0``, ``31``, ``28``, ``31``, ``30``, ``31``, ``30``,``31``, ``31``, ``30``, ``31``, ``30``, ``31` `]``    ` `    ``if` `(isLeap(y)):``        ``month[``2``] ``=` `29``    ``for` `i ``in` `range``(``1``, ``13``):``        ``if` `(offset <``=` `month[i]):``            ``break``        ``offset ``=` `offset ``-` `month[i]``        ` `    ``d[``0``] ``=` `offset``    ``m[``0``] ``=` `i ``+` `1` `# Add x days to the given date.``def` `addDays(d1, m1, y1, x):``    ` `    ``offset1 ``=` `offsetDays(d1, m1, y1)``    ``if` `isLeap(y1):``        ``remDays ``=` `366` `-` `offset1``    ``else``:``        ``remDays ``=` `365` `-` `offset1``        ` `    ``# y2 is going to store result year and``    ``# offset2 is going to store offset days``    ``# in result year.``    ``if` `(x <``=` `remDays):``        ``y2 ``=` `y1``        ``offset2 ``=` `offset1 ``+` `x``    ``else``:``        ` `        ``# x may store thousands of days.``        ``# We find correct year and offset``        ``# in the year.``        ``x ``-``=` `remDays``        ``y2 ``=` `y1 ``+` `1``        ``if` `isLeap(y2):``            ``y2days ``=` `366``        ``else``:``            ``y2days ``=` `365``            ` `        ``while` `(x >``=` `y2days):``            ``x ``-``=` `y2days``            ``y2 ``+``=` `1``            ``if` `isLeap(y2):``                ``y2days ``=` `366``            ``else``:``                ``y2days ``=` `365``        ` `        ``offset2 ``=` `x``        ` `    ``# Find values of day and month from``    ``# offset of result year.``    ``m2 ``=` `[``0``]``    ``d2 ``=` `[``0``]``    ``revoffsetDays(offset2, y2, d2, m2)``    ``print``(``"d2 = "``,``*``d2,``", m2 = "``,``*``m2,``", y2 = "``,y2,sep``=``"")` `# Driven Program``d ``=` `14``m ``=` `3``y ``=` `2015``x ``=` `366` `addDays(d, m, y, x)` `# This code is contributed by shubhamsingh10`

## C#

 `// C# program to find date after adding``// given number of days.``using` `System;``class` `GFG{``    ` `    ``// Find values of day and month from``    ``// offset of result year.``    ``static` `int` `m2, d2;``    ` `// Return if year is leap year or not.``static` `bool` `isLeap(``int` `y)``{``    ``if` `(y % 100 != 0 && y % 4 == 0 || y % 400 == 0)``        ``return` `true``;` `    ``return` `false``;``}` `// Given a date, returns number of days elapsed``// from the beginning of the current year (1st``// jan).``static` `int` `offsetDays(``int` `d, ``int` `m, ``int` `y)``{``    ``int` `offset = d;` `    ``if``(m - 1 == 11)``        ``offset += 335;``    ``if``(m - 1 == 10)``        ``offset += 304;``    ``if``(m - 1 == 9)``        ``offset += 273;``    ``if``(m - 1 == 8)``        ``offset += 243;``    ``if``(m - 1 == 7)``        ``offset += 212;``    ``if``(m - 1 == 6)``        ``offset += 181;``    ``if``(m - 1 == 5)``        ``offset += 151;``    ``if``(m - 1 == 4)``        ``offset += 120;``    ``if``(m - 1 == 3)``        ``offset += 90;``    ``if``(m - 1 == 2)``        ``offset += 59;``    ``if``(m - 1 == 1)``        ``offset += 31;` `    ``if` `(isLeap(y) && m > 2)``        ``offset += 1;` `    ``return` `offset;``}` `// Given a year and days elapsed in it, finds``// date by storing results in d and m.``static` `void` `revoffsetDays(``int` `offset, ``int` `y)``{``    ``int` `[]month = { 0, 31, 28, 31, 30, 31, 30,``                    ``31, 31, 30, 31, 30, 31 };` `    ``if` `(isLeap(y))``        ``month = 29;``    ``int` `i;``    ``for` `(i = 1; i <= 12; i++)``    ``{``        ``if` `(offset <= month[i])``            ``break``;``        ``offset = offset - month[i];``    ``}` `    ``d2 = offset;``    ``m2 = i;``}` `// Add x days to the given date.``static` `void` `addDays(``int` `d1, ``int` `m1, ``int` `y1, ``int` `x)``{``    ``int` `offset1 = offsetDays(d1, m1, y1);``    ``int` `remDays = isLeap(y1) ? (366 - offset1) : (365 - offset1);` `    ``// y2 is going to store result year and``    ``// offset2 is going to store offset days``    ``// in result year.``    ``int` `y2, offset2 = 0;``    ``if` `(x <= remDays)``    ``{``        ``y2 = y1;``        ``offset2 = offset1+x;``    ``}` `    ``else``    ``{``        ``// x may store thousands of days.``        ``// We find correct year and offset``        ``// in the year.``        ``x -= remDays;``        ``y2 = y1 + 1;``        ``int` `y2days = isLeap(y2) ? 366 : 365;``        ``while` `(x >= y2days)``        ``{``            ``x -= y2days;``            ``y2++;``            ``y2days = isLeap(y2)?366:365;``        ``}``        ``offset2 = x;``    ``}``    ``revoffsetDays(offset2, y2);``    ``Console.WriteLine(``"d2 = "` `+ d2 + ``", m2 = "` `+``                        ``m2 + ``", y2 = "` `+ y2);``}` `// Driven Program``static` `void` `Main()``{``    ``int` `d = 14, m = 3, y = 2015;``    ``int` `x = 366;``    ``addDays(d, m, y, x);``}``}``// This code is contributed by mits`

## Javascript

 ``

Output:

`d2 = 14, m2 = 3, y2 = 2016`

This article is contributed by Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.