Skip to content

Latest commit

 

History

History
207 lines (170 loc) · 6.48 KB

16_python_datetime.md

File metadata and controls

207 lines (170 loc) · 6.48 KB

30 Days Of Python: Day 16 - Python Date time

Twitter Follow

Author: Asabeneh Yetayeh
First Edition: Nov 22 - Dec 22, 2019

<< Day 15 | Day 17 >>

30DaysOfPython

📘 Day 16

Python datetime

Python has got datetime module to handle date and time.

import datetime
print(dir(datetime))
['MAXYEAR', 'MINYEAR', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'date', 'datetime', 'datetime_CAPI', 'sys', 'time', 'timedelta', 'timezone', 'tzinfo']

With dir or help built-in commands it is possible to know the available functions in a certain module. As you can see, in the datetime module there are many functions, but we will focus on date, datetime, time and timedelta. Let's see them one by one.

Getting datetime Information

from datetime import datetime
now = datetime.now()
print(now)                      # 2019-12-04 23:34:46.549883
day = now.day                   # 4
month = now.month               # 12
year = now.year                 # 2019
hour = now.hour                 # 23
minute = now.minute             # 38
second = now.second
timestamp = now.timestamp()
print(day, month, year, hour, minute)
print('timestamp', timestamp)
print(f'{day}/{month}/{year}, {hour}:{minute}')  # 4/12/2019, 23:38

Timestamp or Unix timestamp is the number of seconds elapsed from 1st of January 1970 UTC.

Formating Date Output Using strftime

from datetime import datetime
new_year = datetime(2020, 1, 1)
print(new_year)      # 2020-01-01 00:00:00
day = new_year.day
month = new_year.month
year = new_year.year
hour = new_year.hour
minute = new_year.minute
second = new_year.second
print(day, month, year, hour, minute) #1 1 2020 0 0
print(f'{day}/{month}/{year}, {hour}:{minute}')  # 1/1/2020, 0:0

Time formating

from datetime import datetime
# current date and time
now = datetime.now()
t = now.strftime("%H:%M:%S")
print("time:", t)
time_one = now.strftime("%m/%d/%Y, %H:%M:%S")
# mm/dd/YY H:M:S format
print("time one:", time_one)
time_two = now.strftime("%d/%m/%Y, %H:%M:%S")
# dd/mm/YY H:M:S format
print("time two:", time_two)
time: 01:05:01
time one: 12/05/2019, 01:05:01
time two: 05/12/2019, 01:05:01

Here are all the strftime symbols we use to format time. An example of all the formats for this module.

strftime

String to Time Using strptime

from datetime import datetime
date_string = "5 December, 2019"
print("date_string =", date_string)
date_object = datetime.strptime(date_string, "%d %B, %Y")
print("date_object =", date_object)
date_string = 5 December, 2019
date_object = 2019-12-05 00:00:00

Using date from datetime

from datetime import date
d = date(2020, 1, 1)
print(d)
print('Current date:', d.today())    # 2019-12-05
# date object of today's date
today = date.today()
print("Current year:", today.year)   # 2019
print("Current month:", today.month) # 12
print("Current day:", today.day)     # 5

Time Objects to Represent Time

from datetime import time
# time(hour = 0, minute = 0, second = 0)
a = time()
print("a =", a)
# time(hour, minute and second)
b = time(10, 30, 50)
print("b =", b)
# time(hour, minute and second)
c = time(hour=10, minute=30, second=50)
print("c =", c)
# time(hour, minute, second, microsecond)
d = time(10, 30, 50, 200555)
print("d =", d)

output
a = 00:00:00
b = 10:30:50
c = 10:30:50
d = 10:30:50.200555

Difference Between Two Points in Time Using

today = date(year=2019, month=12, day=5)
new_year = date(year=2020, month=1, day=1)
time_left_for_newyear = new_year - today
# Time left for new year:  27 days, 0:00:00
print('Time left for new year: ', time_left_for_newyear)

t1 = datetime(year = 2019, month = 12, day = 5, hour = 0, minute = 59, second = 0)
t2 = datetime(year = 2020, month = 1, day = 1, hour = 0, minute = 0, second = 0)
diff = t2 - t1
print('Time left for new year:', diff) # Time left for new year: 26 days, 23: 01: 00

Difference Between Two Points in Time Using timedelata

from datetime import timedelta
t1 = timedelta(weeks=12, days=10, hours=4, seconds=20)
t2 = timedelta(days=7, hours=5, minutes=3, seconds=30)
t3 = t1 - t2
print("t3 =", t3)
    date_string = 5 December, 2019
    date_object = 2019-12-05 00:00:00
    t3 = 86 days, 22:56:50

🌕 You are extraordinary. You are 16 steps a head to your way to greatness. Now do some exercises for your brain and for your muscle.

💻 Exercises: Day 16

  1. Get the current day, month, year, hour, minute and timestamp from datetime module
  2. Format the current date using this format: "%m/%d/%Y, %H:%M:%S")
  3. Today is 5 December, 2019. Change this time string to time.
  4. Calculate the time difference between now and new year.
  5. Calculate the time difference between 1 January 1970 and now.
  6. Think, what can you use the datetime module for? Examples:
    • Time series analysis
    • To get a timestamp of any activities in an application
    • Adding posts on a blog

🎉 CONGRATULATIONS ! 🎉

<< Day 15 | Day 17 >>