Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

249
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda