-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdate_time.py
65 lines (45 loc) · 1.47 KB
/
date_time.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# -*- coding: utf-8 -*-
import datetime
from dateutil.relativedelta import relativedelta
# Obtener la fecha de hoy
date_now=datetime.date.today()
print(type(date_now))
print(date_now)
# Obtener la fecha junto con la hora de hoy
date_now=datetime.datetime.now()
print(type(date_now))
print(date_now)
# Obtener todas las funciones de: datetime.date
print(dir(datetime.date))
# Obtener todas las funciones de: datetime.datetime
print(dir(datetime.datetime))
hoy = datetime.date.today()
print("Día de la semana: {}".format(hoy.weekday()))
print("Día: {}".format(hoy.day))
print("Año: {}".format(hoy.year))
print("Mes: {}".format(hoy.month))
# Hacer uso de funciones de fecha relativa
# Obtener hoy en 6 meses:
print(hoy + relativedelta(months=+6))
# Obtener hoy en 60 dias:
print(hoy + relativedelta(days=+60))
# Sumar
six_months = datetime.date.today()+ relativedelta( months = +6)
print(six_months)
# Formatear Texto a Objeto fecha:
texto_fecha = 'March 7 2009 7:30pm EST'
texto_fecha = texto_fecha[:-3]
print(texto_fecha)
formatting = "%B %d %Y %I:%M%p "
fecha_formateada = datetime.datetime.strptime(texto_fecha, formatting)
print(fecha_formateada+ datetime.timedelta(hours=12))
# Fecha con isoformat
print(datetime.datetime.now().isoformat())
# Fecha formato UTC
print(datetime.datetime.utcnow())
# Formatear nueva Fecha
fecha_txt = '2017-07-25'
formatting_fecha = "%Y-%m-%d"
nueva_fecha=datetime.datetime.strptime(fecha_txt, formatting_fecha)
print('nueva fecha:')
print(nueva_fecha)