naoe
naoe

Reputation: 21

Time Formating In Python 3.8.2

I'm Trying To Run this code in python 3.8.2 but i have error invalid string so what i'm supposed to used to do it right way this is the code

import datetime 
data =datetime.datetime.now(tz=datetime.timezone.utc).strftime('%Y-%m-%dT%H:%M:%S.%s%z')
print(data)

expect result like this

2020-05-19T15:20:21.327099399+0000

Upvotes: 1

Views: 1113

Answers (1)

medic17
medic17

Reputation: 425

Your error is because datetime doesn't have a definition for %s milisecond only %S for seconds. you can use %f for microseconds

import datetime
data =datetime.datetime.now(tz=datetime.timezone.utc).strftime('%Y%m%dT%H:%M:%S.%f%z')
print(data)

EDIT This should give you milliseconds

import datetime
data =datetime.datetime.now(tz=datetime.timezone.utc).isoformat(timespec='milliseconds')
print(data) 

Upvotes: 1

Related Questions