Fecha y hora de Python
Fechas de Python
Una fecha en Python no es un tipo de datos en sí mismo, pero podemos importar un módulo llamado datetime
para trabajar con fechas como objetos de fecha.
Ejemplo
Importe el módulo de fecha y hora y muestre la fecha actual:
import datetime
x = datetime.datetime.now()
print(x)
Salida de fecha
Cuando ejecutamos el código del ejemplo anterior, el resultado será:
2022-01-29 15:42:54.673131
La fecha contiene año, mes, día, hora, minuto, segundo y microsegundo.
El datetime
módulo tiene muchos métodos para devolver información sobre el objeto de fecha.
Aquí hay algunos ejemplos, aprenderá más sobre ellos más adelante en este capítulo:
Ejemplo
Devuelve el año y el nombre del día de la semana:
import datetime
x = datetime.datetime.now()
print(x.year)
print(x.strftime("%A"))
Creación de objetos de fecha
Para crear una fecha, podemos usar la datetime()
clase (constructor) del
datetime
módulo.
La datetime()
clase requiere tres parámetros para crear una fecha: año, mes, día.
Ejemplo
Crear un objeto de fecha:
import datetime
x = datetime.datetime(2020, 5, 17)
print(x)
La datetime()
clase también toma parámetros para la hora y la zona horaria (hora, minuto, segundo, microsegundo, tzone), pero son opcionales y tienen un valor predeterminado de 0
, ( None
para la zona horaria).
El método strftime()
El datetime
objeto tiene un método para formatear objetos de fecha en cadenas legibles.
El método se llama strftime()
y toma un parámetro,
format
para especificar el formato de la cadena devuelta:
Ejemplo
Mostrar el nombre del mes:
import datetime
x = datetime.datetime(2018, 6, 1)
print(x.strftime("%B"))
Una referencia de todos los códigos de formato legal:
Directive | Description | Example | Try it |
---|---|---|---|
%a | Weekday, short version | Wed | |
%A | Weekday, full version | Wednesday | |
%w | Weekday as a number 0-6, 0 is Sunday | 3 | |
%d | Day of month 01-31 | 31 | |
%b | Month name, short version | Dec | |
%B | Month name, full version | December | |
%m | Month as a number 01-12 | 12 | |
%y | Year, short version, without century | 18 | |
%Y | Year, full version | 2018 | |
%H | Hour 00-23 | 17 | |
%I | Hour 00-12 | 05 | |
%p | AM/PM | PM | |
%M | Minute 00-59 | 41 | |
%S | Second 00-59 | 08 | |
%f | Microsecond 000000-999999 | 548513 | |
%z | UTC offset | +0100 | |
%Z | Timezone | CST | |
%j | Day number of year 001-366 | 365 | |
%U | Week number of year, Sunday as the first day of week, 00-53 | 52 | |
%W | Week number of year, Monday as the first day of week, 00-53 | 52 | |
%c | Local version of date and time | Mon Dec 31 17:41:00 2018 | |
%C | Century | 20 | |
%x | Local version of date | 12/31/18 | |
%X | Local version of time | 17:41:00 | |
%% | A % character | % | |
%G | ISO 8601 year | 2018 | |
%u | ISO 8601 weekday (1-7) | 1 | |
%V | ISO 8601 weeknumber (01-53) | 01 |