66 lines
1.3 KiB
Python
66 lines
1.3 KiB
Python
|
"""
|
||
|
|
||
|
FECHAS (date)
|
||
|
|
||
|
"""
|
||
|
|
||
|
import datetime
|
||
|
|
||
|
# date object
|
||
|
# 18-Jun-20
|
||
|
|
||
|
fecha = datetime.date(2020, 6, 18)
|
||
|
"""
|
||
|
date.year
|
||
|
date.month
|
||
|
date.day
|
||
|
|
||
|
date.weekday() # Lunes 0 -> Domingo 6
|
||
|
date.isoweekday() # Lunes 1 -> Domingo 7
|
||
|
date.isocalendar() # tuple( año, semana, dia semana)
|
||
|
date.isoformat() # YYY-MM-DD
|
||
|
"""
|
||
|
|
||
|
hoy = datetime.date.today()
|
||
|
|
||
|
ayer = hoy - datetime.timedelta(days=1)
|
||
|
|
||
|
delta = hoy - ayer
|
||
|
|
||
|
|
||
|
# DATETIME ( hora y fechas )
|
||
|
|
||
|
fecha_hora = datetime.datetime(2020, 12, 2, 9, 15, 30)
|
||
|
# (YYYY, MM, D, h, m , s )
|
||
|
print(fecha_hora.date())
|
||
|
|
||
|
# formato local
|
||
|
ahora = datetime.datetime.now()
|
||
|
|
||
|
# UTC
|
||
|
ahora = datetime.datetime.utcnow()
|
||
|
|
||
|
"""
|
||
|
TIME
|
||
|
|
||
|
hora = datetime.time(10, 40, 35)
|
||
|
hora.hour
|
||
|
hora.minute
|
||
|
hora.second
|
||
|
hora.microsecond
|
||
|
|
||
|
CONVERSION
|
||
|
|
||
|
fecha = datetime.datetime(2020, 4, 30, 11, 25, 30)
|
||
|
|
||
|
Conversion DateTime a String # strftime
|
||
|
- fecha.strftime('%Y-%m-%d')
|
||
|
- fecha.strftime('%Y-%m-%d' T%H:%M:%S')
|
||
|
- fecha.strftime('%Y-%m-%d %H:%M:%S')
|
||
|
|
||
|
Conversion String a DateTime # strptime
|
||
|
- fecha.strptime('2020-01-10', '%Y-%m-%d')
|
||
|
- fecha.strptime('2020-01-10 T11:30:25', '%Y-%m-%d' T%H:%M:%S')
|
||
|
- fecha.strptime('2020-01-10 11:30:25','%Y-%m-%d %H:%M:%S')
|
||
|
"""
|