Data Science Academy - Python Fundamentos - Capítulo 4

Download: http://github.com/dsacademybr


In [1]:
# Versão da Linguagem Python
from platform import python_version
print('Versão da Linguagem Python Usada Neste Jupyter Notebook:', python_version())


Versão da Linguagem Python Usada Neste Jupyter Notebook: 3.7.6

Datetime

Esse módulo permite a manipulação de datas em Python.


In [2]:
import datetime

In [3]:
agora = datetime.datetime.now()

In [4]:
agora


Out[4]:
datetime.datetime(2020, 3, 23, 16, 22, 19, 718745)

In [5]:
t = datetime.time(7, 43, 28)

In [6]:
print (t)


07:43:28

In [7]:
print ('Hora  :', t.hour)
print ('Minute:', t.minute)
print ('Segundo:', t.second)
print ('Microsegundo:', t.microsecond)


Hora  : 7
Minute: 43
Segundo: 28
Microsegundo: 0

In [8]:
print(datetime.time.min)


00:00:00

In [9]:
hoje = datetime.date.today()

In [10]:
print (hoje)
print ('ctime:', hoje.ctime())
print ('Ano:', hoje.year)
print ('Mês :', hoje.month)
print ('Dia :', hoje.day)


2020-03-23
ctime: Mon Mar 23 00:00:00 2020
Ano: 2020
Mês : 3
Dia : 23

In [11]:
d1 = datetime.date(2015, 4, 28)
print ('d1:', d1)


d1: 2015-04-28

In [12]:
d2 = d1.replace(year=2016)
print ('d2:', d2)


d2: 2016-04-28

In [13]:
# Diferença em dias entre duas datas
d2 - d1


Out[13]:
datetime.timedelta(days=366)

FIM

Obrigado - Data Science Academy - facebook.com/dsacademybr