2018年1月8日 星期一

Python: Calculate number of days between two dates

Python: Calculate number of days between two dates

Python Basic: Exercise-14 with Solution

Write a Python program to calculate number of days between two dates.
Python datetime.date(year, month, day) :
The function returns date object with same year, month and day. All arguments are required. Arguments may be integers, in the following ranges:
  • MINYEAR <= year <= MAXYEAR
  • 1 <= month <= 12
  • 1 <= day <= number of days in the given month and year
If an argument outside those ranges is given, ValueError is raised.
  • Note: The smallest year number allowed in a date or datetime object. MINYEAR is 1.
  • The largest year number allowed in a date or datetime object. MAXYEAR is 9999.
Sample Solution:-
from datetime import date
f_date = date(2017, 12, 1)
l_date = date(2018, 1, 8)
delta = l_date - f_date
print(delta.days)

=========== RESTART: F:/Python_APSC/py-ex-basic-14.py ==========
38
>>> 

沒有留言:

張貼留言

DHT11 (Node-Red) +PostgreSQL 模擬

 DHT11 (Node-Red) +PostgreSQL 模擬 [{"id":"acddd911a6412f0a","type":"inject","z":"08dc4...