Answer:
Python datetime [52 exercises with solution]
[An editor is available at the bottom of the page to write and execute the scripts.]
1. Write a Python script to display the various Date Time formats - Go to the editor
a) Current date and time
b) Current year
c) Month of year
d) Week number of the year
e) Weekday of the week
f) Day of year
g) Day of the month
h) Day of week
Click me to see the solution
2. Write a Python program to determine whether a given year is a leap year. Go to the editor
Click me to see the solution
3. Write a Python program to convert a string to datetime. Go to the editor
Sample String : Jan 1 2014 2:43PM
Expected Output : 2014-07-01 14:43:00
Click me to see the solution
4. Write a Python program to get the current time in Python. Go to the editor
Sample Format : 13:19:49.078205
Click me to see the solution
5. Write a Python program to subtract five days from current date. Go to the editor
Sample Date :
Current Date : 2015-06-22
5 days before Current Date : 2015-06-17
Click me to see the solution
6. Write a Python program to convert unix timestamp string to readable date. Go to the editor
Sample Unix timestamp string : 1284105682
Expected Output : 2010-09-10 13:31:22
Click me to see the solution
7. Write a Python program to print yesterday, today, tomorrow. Go to the editor
Click me to see the solution
8. Write a Python program to convert the date to datetime (midnight of the date) in Python. Go to the editor
Sample Output : 2015-06-22 00:00:00
Click me to see the solution
9. Write a Python program to print next 5 days starting from today. Go to the editor
Click me to see the solution
10. Write a Python program to add 5 seconds with the current time. Go to the editor
Sample Data :
13:28:32.953088
13:28:37.953088
Click me to see the solution
11. Write a Python program to convert Year/Month/Day to Day of Year in Python. Go to the editor
Click me to see the solution
12. Write a Python program to get current time in milliseconds in Python Go to the editor
Click me to see the solution
13. Write a Python program to get week number. Go to the editor
Sample Date : 2015, 6, 16
Expected Output : 25
Click me to see the solution
14. Write a Python program to find the date of the first Monday of a given week. Go to the editor
Sample Year and week : 2015, 50
Expected Output : Mon Dec 14 00:00:00 2015
Click me to see the solution
15. Write a Python program to select all the Sundays of a specified year. Go to the editor
Click me to see the solution
16. Write a Python program to add year(s) with a given date and display the new date. Go to the editor
Sample Data : (addYears is the user defined function name)
print(addYears(datetime.date(2015,1,1), -1))
print(addYears(datetime.date(2015,1,1), 0))
print(addYears(datetime.date(2015,1,1), 2))
print(addYears(datetime.date(2000,2,29),1))
Expected Output :
2014-01-01
2015-01-01
2017-01-01
2001-03-01
Click me to see the solution
17. Write a Python program to drop microseconds from datetime. Go to the editor
Click me to see the solution
18. Write a Python program to get days between two dates. Go to the editor
Sample Dates : 2000,2,28, 2001,2,28
Expected Output : 366 days, 0:00:00
Click me to see the solution
19. Write a Python program to get the date of the last Tuesday. Go to the editor
Click me to see the solution
20. Write a Python program to test the third Tuesday of a month. Go to the editor
Click me to see the solution
21. Write a Python program to get the last day of a specified year and month. Go to the editor
Click me to see the solution
22. Write a Python program to get the number of days of a given month and year. Go to the editor
Click me to see the solution
23. Write a Python program to add a month with a specified date. Go to th
Explanation: