0

I send django datetime field data to client in string type

my question is how i can convert this string to python datetime?

my datetime is: "2019-07-22T10:01:40.876487+04:30"

I want to do something like this:

datatime.datetime('2019-07-22T10:01:40.876487+04:30', '<django default datetime format>')

and I expect to have:

<class 'datetime.datetime'>

WHAT IS DJANGO DEFAULT DATETIME FORMAT?

OR HOW I CAN CONVERT THIS STRING TO DATETIME?

ehsan ahmadi
  • 365
  • 2
  • 13
  • probably this will help: https://docs.djangoproject.com/en/2.2/ref/settings/#std:setting-DATETIME_FORMAT – ruddra Jul 22 '19 at 08:51
  • For the record, that datetime format is the gold standard of datetime formats, ISO8601. – AKX Jul 22 '19 at 08:52
  • Possible duplicate of [Parse date string and change format](https://stackoverflow.com/questions/2265357/parse-date-string-and-change-format) – vekerdyb Jul 22 '19 at 08:53

1 Answers1

1

You can use django's dateparse library, which is fairly comprehensive and robust wrt the iso 8601 formats:

from django.utils.dateparse import parse_datetime

datetime_obj = parse_datetime('2019-07-22T10:01:40.876487+04:30')
# datetime.datetime(2019, 7, 22, 10, 1, 40, 876487, tzinfo=<django.utils.timezone.FixedOffset object at 0x7f9251844f98>)
user2390182
  • 72,016
  • 6
  • 67
  • 89