S.K
S.K

Reputation: 530

How to get current datetime in this format 2020-01-13T09:25:19-0330 in Python?

What is this date format 2020-01-13T09:25:19-0330 ? and how can I get the current datetime in this format in python ?

Edited: Also note there are only 4 digits after last -. The API which I need to hit accepts exactly this format.

2nd Edit: Confirmed from api's dev team, last 4 digits are milliseconds, with 0 prepended. ex, 330 is the milliseconds, and they mention it as 0330.

Upvotes: 0

Views: 1591

Answers (1)

Bharel
Bharel

Reputation: 26901

It's an ISO 8601 timestamp format.

In order to get the current time in that format:

from datetime import datetime
print(datetime.now().isoformat())

In your case, the iso format is truncated to seconds, and has a timezone:

from datetime import datetime, timezone, timedelta
tz = timezone(timedelta(hours=-3.5))
current_time = datetime.now(tz)
print(current_time.isoformat(timespec="seconds"))

Where -3.5 is the UTC offset.


If you wish to use the system's local timezone, you can do so like this:

from datetime import datetime, timezone, timedelta
current_time = datetime.now().astimezone()
print(current_time.isoformat(timespec="seconds"))

Upvotes: 3

Related Questions