Time Island
Now let us set our compass to our third and final island – Time
Island!
If only we can fast forward time to get there quicker!
And we have another map to help us seek our treasure to solve all our problems in our lifetime! Here is the map for the datetime module.
Now, we have arrived at Time
Island. That took much less time than I expected!
I’m going to explore the island - looks like a good day to be swimming. And I heard that there might be treasures underwater too, lost in time.
I’ve written details about your mission on this scroll. Read them carefully, and please deliver your mission on time! I expect everything to be done once I’m back from my swim!
Firstly, let’s all synchronise our date and time!
import datetime
Ok, off you go!
Mission #1: What is the current date and time?
Firstly, can you have Python tell me the current date and time please?
Mission #2: A new birth date and time!
Now, create a new instance of datetime
with your birth date and time. Feel free to fake this. I don’t care!
Mission #3: Separate the day, month and year.
I would like you to show me your birth day, month and year separately, using the datetime
instance from Mission #2.
Mission #4: How old are you now?
I want to know how old you are. Can you get Python to tell me the difference between the current datetime
and your birth datetime
?
Can you also try to figure out what that resulting timedelta
thing is?
Mission #5: Format the datetime
I need the date written out nicely in the official format set out by the authorities in Great Britannia. Can you display today’s datetime
as a string in this format please? 05/10/2020 14:11:36
Mission #6: Bring back the datetime
Now, can you convert the string that you created in Mission #5 back to a datetime
object please?
Mission #7: Just a date
I now do not care about the time. Can you get Python to give me only the date for today please? I want it to return me just a date
object, not a datetime
object.
Mission #8: Exploring different parallel universes
Now sailing is a big business, and we need to be open and explore different islands in different timezones. Can you create a new datetime
object for the current date/time in the UTC+8 timezone please? Print out the string like in Mission #5 to show me the current time in UTC+8 (should be 8 hours ahead of Great Britannia).
…
Ok, I’m back from my swim. Unfortunately I did not find any lost treasure under the sea. How did you fare? Have you completed all your missions? Excellent to hear that you completed them all in good time! Congratulations. I now hereby promote you to First Mate of the crew! Make me proud!
Now, let’s sail back to Great Britannia where more snake adventures await us!