Importing Packages in Python

Let's import a package

import datetime

Use ipython magic to see function options. Type datetime. and press tab. Highlight time and press enter. Hit shift-tab

dt.time
datetime.time
Object `datetime.time` not found.

Use a function from the datetime package

print (datetime.time)

NameErrorTraceback (most recent call last)
<ipython-input-11-8c14636d5769> in <module>()
----> 1 print (datetime.time)

NameError: name 'datetime' is not defined

Datetime Package

Use the now function to get the current datetime stamp.

 

Extract the day from the timestamp

 

Extract some other datetime elements

 

Create a timestamp for Christmas

 

Do math with the timestamps

 

Convert a string to datetime format

 

Convert a datetime object to a string

 

Exercises

1. Create a variable called future with a dateime value of January 15, 2016 at 5:30pm

 

2. Create a variable with a timedelta equal to the difference between future and now

 

3. Print the number of seconds from the timedelta

 

Bonus: Try converting strings to datetime objects and datetime objects back to strings. What are some interesting datetime formats you can find in the documentation?