Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

252
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!