VOOZH about

URL: https://www.geeksforgeeks.org/dsa/date-after-adding-given-number-of-days-to-the-given-date/

⇱ Date after adding given number of days to the given date - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Date after adding given number of days to the given date

Last Updated : 22 Dec, 2022

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

Examples: 

Input : d1 = 14, m1 = 5, y1 = 2017, x = 10
Output : d2 = 24, m2 = 5, y2 = 2017

Input : d1 = 14, m1 = 3, y1 = 2015, x = 366
Output : d2 = 14, m2 = 3, y2 = 2016

Method 1: 

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. 


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

Time complexity: O(1) as it is performing constant operations
Auxiliary space: O(1)


Comment
Article Tags: