Advertisement
furas

Python - add 3 months to datetime

Dec 25th, 2017
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.54 KB | None | 0 0
  1. # Two methods to get new date
  2. #  1. after 90 days
  3. #  2. after 3 months
  4.  
  5. #---------------------------------------------------------------
  6. # 1. after 90 days
  7. #---------------------------------------------------------------
  8.  
  9. import datetime
  10.  
  11. range_90_days = datetime.timedelta(days=90)
  12.  
  13. date1 = datetime.datetime.strptime('09.10.2016', "%d.%m.%Y")
  14. date2 = date1 + range_90_days
  15. date3 = date2 + range_90_days
  16. date4 = date3 + range_90_days
  17.  
  18. print(date1.strftime("%d.%m.%Y"))
  19. print(date2.strftime("%d.%m.%Y"))
  20. print(date3.strftime("%d.%m.%Y"))
  21. print(date4.strftime("%d.%m.%Y"))
  22. print('-----')
  23.  
  24. # 09.10.2016
  25. # 07.01.2017
  26. # 07.04.2017
  27. # 06.07.2017
  28.  
  29. #---------------------------------------------------------------
  30. # 2. after 3 months
  31. #---------------------------------------------------------------
  32.  
  33. import datetime
  34.  
  35. date1 = datetime.datetime.strptime('09.10.2016', "%d.%m.%Y")
  36.  
  37. year = date1.year
  38. month = date1.month
  39. month += 3
  40. if month > 12:
  41.     month -= 12
  42.     year += 1
  43.  
  44. date2 = date1.replace(month=month, year=year)
  45.  
  46. year = date2.year
  47. month = date2.month
  48. month += 3
  49. if month > 12:
  50.     month -= 12
  51.     year += 1
  52.  
  53. date3 = date2.replace(month=month, year=year)
  54.  
  55. year = date3.year
  56. month = date3.month
  57. month += 3
  58. if month > 12:
  59.     month -= 12
  60.     year += 1
  61.  
  62. date4 = date3.replace(month=month, year=year)
  63.  
  64. print(date1.strftime("%d.%m.%Y"))
  65. print(date2.strftime("%d.%m.%Y"))
  66. print(date3.strftime("%d.%m.%Y"))
  67. print(date4.strftime("%d.%m.%Y"))
  68. print('-----')
  69.  
  70. # 09.10.2016
  71. # 09.01.2017
  72. # 09.04.2017
  73. # 09.07.2017
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement