0

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.

S.K
  • 480
  • 1
  • 4
  • 19
  • Does this answer your question? [How to get the current time in Python](https://stackoverflow.com/questions/415511/how-to-get-the-current-time-in-python) – code11 Sep 02 '20 at 16:38
  • concerning the 2nd Edit, please tell the api dev team to have a look at [iso 8601](https://en.wikipedia.org/wiki/ISO_8601) and [rfc 3339](https://tools.ietf.org/html/rfc3339) - there are reasons why you *don't* want to re-invent the wheel ;-) – FObersteiner Sep 03 '20 at 06:44

1 Answers1

3

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"))
Bharel
  • 23,672
  • 5
  • 40
  • 80
  • thanks. It is similar. But the api which I need to hit, wants the last %fZ to be only of 4 digits. I mean it can accept ```2020-01-13T09:25:19-0330``` but not ```2020-01-13T09:25:19-0330123``` – S.K Sep 02 '20 at 16:43
  • @S.K It's without microseconds and with a timezone. See my edit. – Bharel Sep 02 '20 at 16:53
  • Thanks @Bharel. I tried it. But it gives 2 digits for seconds. I dont know what the last 4 digits are for. I checked timespec documentation and tried all options for it including milliseconds and microseconds. But none of them gives 4 digits. – S.K Sep 03 '20 at 03:16
  • I have confirmed from the api's dev support team for the last 4 digits. Have edited the question for that. Please have a look. Thanks. I think I will need to format the time string manually using string replace or something. Please suggest if you have a better option. – S.K Sep 03 '20 at 04:14