Python: Create 12 fixed dates from a specified date over a given period
Python Datetime: Exercise-27 with Solution
Write a Python program to create 12 fixed dates from a specified date over a given period. The difference between two dates will be 20.
Sample Solution:
Python Code:
import datetime
def every_20_days(date):
print('Starting Date: {d}'.format(d=date))
print("Next 12 days :")
for _ in range(12):
date=date+datetime.timedelta(days=20)
print('{d}'.format(d=date))
dt = datetime.date(2016,8,1)
every_20_days(dt)
Sample Output:
Starting Date: 2016-08-01 Next 12 days : 2016-08-21 2016-09-10 2016-09-30 2016-10-20 2016-11-09 2016-11-29 2016-12-19 2017-01-08 2017-01-28 2017-02-17 2017-03-09 2017-03-29
Flowchart:
Visualize Python code execution:
The following tool visualize what the computer is doing step-by-step as it executes the said program:
Python Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a Python program calculates the date six months from the current date using the datetime module.
Next: Write a Python program to get the dates 30 days before and after from the current date.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
Python: Tips of the Day
Find current directory and file's directory:
To get the full path to the directory a Python file is contained in, write this in that file:
import os dir_path = os.path.dirname(os.path.realpath(__file__))
(Note that the incantation above won't work if you've already used os.chdir() to change your current working directory, since the value of the __file__ constant is relative to the current working directory and is not changed by an os.chdir() call.)
To get the current working directory use
import os cwd = os.getcwd()
Documentation references for the modules, constants and functions used above:
- The os and os.path modules.
- The __file__ constant
- os.path.realpath(path) (returns "the canonical path of the specified filename, eliminating any symbolic links encountered in the path")
- os.path.dirname(path) (returns "the directory name of pathname path")
- os.getcwd() (returns "a string representing the current working directory")
- os.chdir(path) ("change the current working directory to path")
Ref: https://bit.ly/3fy0R6m
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework