Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

251
Vistas
Using datetime.now gives datetime.datetime error

so i've been on the python language making stuff. I encounter some error which is not so understandable:

TypeError: 'datetime.datetime' object is not subscriptable

(I think) this is the code that cause it:

def brew():
    # Get UTC time
    curr_dt = datetime.now(timezone.utc)
    # Slice it
    curr_dt = curr_dt[0:19]
    # Print
    print(curr_dt)

What am I doing wrong? I just want to get the date and time in 2022-01-04 14:25:10.860837+00:00 by slicing it. Is there a solution of how to get rid of error or even comes with more easy and/or practical ways? Thank You for Your time.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Use the following code:

from datetime import datetime, timezone

def brew():
    # curr_dt = datetime.now(timezone.utc).strftime('%Y-%m-%d %H:%M:%S')
    curr_dt = str(datetime.now(timezone.utc))[:19]
    print(curr_dt)

Output:

>>> brew()
'2022-01-04 14:35:04'
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda