Given two dates, find the total number of days between them.
Note: The system follows Gregorian calender from the beginning of time.
 
Example 1:
Input:
d1 = 10, m1 = 2, y1 = 2014
d2 = 10, m2 = 3, y2 = 2015
Output:
393
Explanation:
By counting manually, we find out there
are 393 days between the two dates.
Example 2:
Input:
d1 = 10, m1 = 2, y1 = 2001
d2 = 10, m2 = 2, y2 = 2002
Output:
365
Explanation:
By counting manually, we find out there
are 365 days between the two dates.
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function noOfDays() which takes Integers d1,m1,y1 for the first date and d2,m2,y2 as the second date as input and returns the answer.
 
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
 
Constraints:
1 <= d1,d2 <= 31
1 <= m1,m2 <= 12
1 <= y1,y2 <= 3000

Source: geeksforgeeks | Difficulty: EASY
Tags: Mathematics

Starter code: #User function Template for python3



class Solution:

    def noOfDays(self, d1, m1, y1, d2, m2, y2):

        # code here 

Solutions:
from datetime import date

class Solution:

	def noOfDays(self, d1, m1, y1, d2, m2, y2):
		d1 = date(y1, m1, d1)
		d2 = date(y2, m2, d2)
		if d2 > d1:
			delta = d2 - d1
		else:
			delta = d1 - d2
		return delta.days


from datetime import date

class Solution:

	def noOfDays(self, d1, m1, y1, d2, m2, y2):
		d0 = date(y1, m1, d1)
		d1 = date(y2, m2, d2)
		delta = d1 - d0
		return abs(delta.days)


class Solution:

	def noOfDays(self, d1, m1, y1, d2, m2, y2):
		months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
		days = 0

		def calcDays(y1, m1, d1, days):
			for yr in range(int(y1)):
				if yr % 4 == 0 and (yr % 100 != 0 or yr % 400 == 0):
					days += 366
				else:
					days += 365
			for mths in range(0, int(m1) - 1):
				days = days + months[mths]
			if m1 > 2 and y1 % 4 == 0 and (y1 % 100 != 0 or y1 % 400 == 0):
				days += 1
			days = days + d1
			return days
		day1 = calcDays(y1, m1, d1, 0)
		day2 = calcDays(y2, m2, d2, 0)
		return abs(day2 - day1)


class Solution:

	def noOfDays(self, d1, m1, y1, d2, m2, y2):

		def countLeapYears(y, m):
			years = y
			if m <= 2:
				years -= 1
			ans = int(years / 4)
			ans -= int(years / 100)
			ans += int(years / 400)
			return ans
		monthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
		n1 = y1 * 365 + d1
		for i in range(0, m1 - 1):
			n1 += monthDays[i]
		n1 += countLeapYears(y1, m1)
		n2 = y2 * 365 + d2
		for q in range(0, m2 - 1):
			n2 += monthDays[q]
		n2 += countLeapYears(y2, m2)
		return int(abs(n2 - n1))


class Solution:

	def noOfDays(self, d1, m1, y1, d2, m2, y2):
		import datetime
		dt1 = datetime.date(y1, m1, d1)
		dt2 = datetime.date(y2, m2, d2)
		return abs((dt2 - dt1).days)


class Solution:

	def noOfDays(self, d1, m1, y1, d2, m2, y2):
		year1 = y1
		month1 = m1
		day1 = d1
		year2 = y2
		month2 = m2
		day2 = d2
		num1 = self.NumDaysBeforeDate(year1, month1, day1)
		num2 = self.NumDaysBeforeDate(year2, month2, day2)
		return abs(num2 - num1)

	def NumDaysBeforeDate(self, year, month, day):
		numOfDays = year * 365 + day
		for currMonth in range(1, month):
			numOfDays += self.DaysInMonth(currMonth)
		numOfDays += self.numberOfLeapYears(month, year)
		return numOfDays

	def numberOfLeapYears(self, month, year):
		if month <= 2:
			year -= 1
		countOfLeapYears = year // 4 - year // 100 + year // 400
		return countOfLeapYears

	def DaysInMonth(self, month):
		DaysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
		return DaysInMonth[month - 1]


class Solution:

	def l(self, m, y):
		years = y
		if m <= 2:
			years -= 1
		a = int(years / 4)
		a -= int(years / 100)
		a += int(years / 400)
		return a

	def noOfDays(self, d1, m1, y1, d2, m2, y2):
		md = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
		n1 = y1 * 365 + d1
		for i in range(m1 - 1):
			n1 += md[i]
		n1 += self.l(m1, y1)
		n2 = y2 * 365 + d2
		for i in range(m2 - 1):
			n2 += md[i]
		n2 += self.l(m2, y2)
		return abs(n2 - n1)


import datetime

class Solution:

	def noOfDays(self, d1, m1, y1, d2, m2, y2):
		a = datetime.datetime(y2, m2, d2, 0, 0, 0)
		b = datetime.datetime(y1, m1, d1, 0, 0, 0)
		c = a - b
		ans = ''
		s = str(c)
		if s[0] == '0':
			return '0'
		for i in s:
			if i != ' ':
				ans = ans + i
			else:
				break
		return abs(int(ans))


class Solution:
	dici = {1: 31, 2: 28, 3: 31, 4: 30, 5: 31, 6: 30, 7: 31, 8: 31, 9: 30, 10: 31, 11: 30, 12: 31}

	def ly(self, m, y):
		if m <= 2:
			y -= 1
		return y // 4 - y // 100 + y // 400

	def sd(self, d, m, y):
		temp = y * 365
		for i in range(1, m):
			temp += self.dici[i]
		temp += self.ly(m, y)
		temp += d
		return temp

	def noOfDays(self, d1, m1, y1, d2, m2, y2):
		return abs(self.sd(d2, m2, y2) - self.sd(d1, m1, y1))


from datetime import date

class Solution:

	def noOfDays(self, d1, m1, y1, d2, m2, y2):
		date2 = date(y2, m2, d2)
		date1 = date(y1, m1, d1)
		d = abs(date2 - date1)
		return d.days


from datetime import date

class Solution:

	def noOfDays(self, d1, m1, y1, d2, m2, y2):
		f_date = date(y1, m1, d1)
		l_date = date(y2, m2, d2)
		delta = l_date - f_date
		return abs(delta.days)
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		(d1, m1, y1) = map(int, input().split())
		(d2, m2, y2) = map(int, input().split())
		ob = Solution()
		print(ob.noOfDays(d1, m1, y1, d2, m2, y2))


class Solution:

	def checkYear(self, year):
		if year % 4 == 0:
			if year % 100 == 0:
				if year % 400 == 0:
					return True
				else:
					return False
			else:
				return True
		else:
			return False

	def daysInMonth(self, month, year):
		if self.checkYear(year) and month == 2:
			return 29
		elif month == 2:
			return 28
		elif month <= 7:
			if month % 2 == 0:
				return 30
			else:
				return 31
		elif month % 2 == 0:
			return 31
		else:
			return 30

	def noOfDays(self, start_day, start_month, star_year, end_day, end_month, en_year):
		start_year = min(star_year, en_year)
		end_year = max(star_year, en_year)
		if start_year != star_year:
			(start_day, end_day) = (end_day, start_day)
			(start_month, end_month) = (end_month, start_month)
		elif start_year == end_year and start_month > end_month:
			(start_day, end_day) = (end_day, start_day)
			(start_month, end_month) = (end_month, start_month)
		if start_year == end_year:
			if start_month != end_month:
				totalDays = self.daysInMonth(start_month, start_year) - start_day + end_day
				for i in range(start_month + 1, end_month):
					totalDays += self.daysInMonth(i, start_year)
			else:
				totalDays = end_day - start_day
		else:
			totalDays = self.daysInMonth(start_month, start_year) - start_day + end_day
			for j in range(start_month + 1, 13):
				totalDays += self.daysInMonth(j, start_year)
			for k in range(1, end_month):
				totalDays += self.daysInMonth(k, end_year)
			for i in range(start_year + 1, end_year):
				if self.checkYear(i):
					totalDays += 366
				else:
					totalDays += 365
		return totalDays
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		(d1, m1, y1) = map(int, input().split())
		(d2, m2, y2) = map(int, input().split())
		ob = Solution()
		print(ob.noOfDays(d1, m1, y1, d2, m2, y2))


class Solution:

	def countleapyears(self, d, m, y):
		if m <= 2:
			y -= 1
		return y // 4 - y // 100 + y // 400

	def calculatedays(self, d, m, y):
		daysinmonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
		n = y * 365 + d
		for i in range(0, m - 1):
			n += daysinmonth[i]
		n += self.countleapyears(d, m, y)
		return n

	def noOfDays(self, d1, m1, y1, d2, m2, y2):
		return abs(self.calculatedays(d2, m2, y2) - self.calculatedays(d1, m1, y1))


class Solution:
	month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

	def leap_count(self, m, y):
		year = y
		if m <= 2:
			y -= 1
		return int(y / 4) - int(y / 100) + int(y / 400)

	def noOfDays(self, d1, m1, y1, d2, m2, y2):
		n1 = y1 * 365 + d1
		for x in range(m1 - 1):
			n1 += Solution.month_days[x]
		n1 += self.leap_count(m1, y1)
		n2 = y2 * 365 + d2
		for x in range(m2 - 1):
			n2 += Solution.month_days[x]
		n2 += self.leap_count(m2, y2)
		return abs(n2 - n1)


from datetime import date

class Solution:

	def noOfDays(self, d1, m1, y1, d2, m2, y2):
		d0 = date(y1, m1, d1)
		d1 = date(y2, m2, d2)
		delta = d1 - d0
		ans = delta.days
		return ans if ans > 0 else -1 * ans


class Solution:

	def noOfDays(self, d1, m1, y1, d2, m2, y2):

		def addLeap(y, m):
			if m <= 2:
				y -= 1
			return y // 4 + y // 400 - y // 100
		f = y1 * 365 + d1
		s = y2 * 365 + d2
		month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
		for i in range(m1 - 1):
			f += month[i]
		for i in range(m2 - 1):
			s += month[i]
		f += addLeap(y1, m1)
		s += addLeap(y2, m2)
		return abs(f - s)


class Solution:

	def noOfDays(self, d1, m1, y1, d2, m2, y2):

		def addleap(y, m):
			if m <= 2:
				y -= 1
			return y // 4 + y // 400 - y // 100
		first = y1 * 365 + d1
		second = y2 * 365 + d2
		month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
		for i in range(m1 - 1):
			first += month[i]
		for j in range(m2 - 1):
			second += month[j]
		first += addleap(y1, m1)
		second += addleap(y2, m2)
		return abs(second - first)


import datetime as dt

class Solution:

	def noOfDays(self, d1, m1, y1, d2, m2, y2):
		date_format1 = dt.datetime(y1, m1, d1)
		date_format2 = dt.datetime(y2, m2, d2)
		if y2 > y1:
			delta = date_format2 - date_format1
		elif y2 == y1 and m2 > m1:
			delta = date_format2 - date_format1
		elif y2 == y1 and m1 > m2:
			delta = date_format1 - date_format2
		else:
			delta = date_format1 - date_format2
		return delta.days


from datetime import date

class Solution:

	def noOfDays(self, d1, m1, y1, d2, m2, y2):
		f_date = date(y1, m1, d1)
		l_date = date(y2, m2, d2)
		delta = l_date - f_date
		if delta.days < 0:
			return -1 * delta.days
		return delta.days


class Solution:

	def countLeapYears(self, m, y) -> int:
		sol = 0
		years = y
		if m <= 2:
			years -= 1
		sol = years // 4
		sol -= years // 100
		sol += years // 400
		return sol

	def getDaysInMonth(self, month) -> int:
		MONTHS_WITH_31_DAYS = {1, 3, 5, 7, 8, 10, 12}
		if month in MONTHS_WITH_31_DAYS:
			return 31
		elif month == 2:
			return 28
		else:
			return 30

	def noOfDays(self, d1, m1, y1, d2, m2, y2):

		def calculateAbsoluteDays(y, m, d):
			totalDays = y * 365 + d
			for i in range(1, m):
				totalDays += self.getDaysInMonth(i)
			totalDays += self.countLeapYears(m, y)
			return totalDays
		date1 = calculateAbsoluteDays(y1, m1, d1)
		date2 = calculateAbsoluteDays(y2, m2, d2)
		return abs(date1 - date2)


from datetime import date

class Solution:

	def noOfDays(self, d1, m1, y1, d2, m2, y2):
		date1 = date(y1, m1, d1)
		date2 = date(y2, m2, d2)
		ans = (date2 - date1).days
		ans = str(ans)
		if ans[0] == '-':
			return ans[1:]
		else:
			return ans


import datetime

class Solution:

	def noOfDays(self, d1, m1, y1, d2, m2, y2):
		x1 = datetime.date(y1, m1, d1)
		x2 = datetime.date(y2, m2, d2)
		x = x2 - x1
		return abs(x.days)


from datetime import date

class Solution:

	def noOfDays(self, d1, m1, y1, d2, m2, y2):
		f = date(y1, m1, d1)
		l = date(y2, m2, d2)
		d = l - f
		return abs(d.days)


from datetime import date

class Solution:

	def noOfDays(self, d1, m1, y1, d2, m2, y2):
		mm = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

		def countleapyear(d, m, y):
			yy = y
			if m <= 2:
				yy -= 1
			return yy // 400 + yy // 4 - yy // 100
		n1 = 365 * y1 + d1 + sum(mm[0:m1 - 1]) + countleapyear(d1, m1, y1)
		n2 = 365 * y2 + d2 + sum(mm[0:m2 - 1]) + countleapyear(d2, m2, y2)
		return abs(n1 - n2)


from datetime import date

class Solution:

	def noOfDays(self, d1, m1, y1, d2, m2, y2):
		t1 = date(y1, m1, d1)
		t2 = date(y2, m2, d2)
		return abs(t1 - t2).days


from datetime import date

class Solution:

	def noOfDays(self, d1, m1, y1, d2, m2, y2):
		s = date(y1, m1, d1)
		e = date(y2, m2, d2)
		return abs((e - s).days)