Parsing a Datetime String into a Django DateTimeField

前端 未结 5 1835
盖世英雄少女心
盖世英雄少女心 2020-12-05 06:30

I have a Django app with a model that contains a field of type DateTimeField.
I am pulling data from the web in the format of 2008-04-10 11:47:58-05.
I

5条回答
  •  佛祖请我去吃肉
    2020-12-05 07:09

    String format of Django DateTimeField is "%Y-%m-%dT%H:%M:%S.%fZ". Hence, conversion between eachother can be done using strptime() or strptime() using this format.

    eg. for string formatted value (2016-10-03T19:00:00.999Z), it can be converted to Django datetime object as :

    from datetime import datetime
    
    datetime_str = '2016-10-03T19:00:00.999Z'
    
    datetime_object = datetime.strptime(datetime_str, "%Y-%m-%dT%H:%M:%S.%fZ")
    

提交回复
热议问题