0

I am trying to write date-time to MongoDB from python.

    time_second = ('{0}-{1}-{2} {3}:{4}:{5}.{6}{7}'.format(t_year, t_month, t_day, t_hour, t_minute, t_second, t_ms, t_us))
    date = datetime.strptime(time_second, "%Y-%m-%d %H:%M:%S.%f")
    date_json = json.dumps(date)

I am getting the TypeError:

  • As the error message says, you cannot have a datetime object in json file. – taras Oct 10 '18 at 14:11
  • Unless this is just a really strange example... it'd be a lot easier to create `date` directly with `datetime(t_year, ...)`. – kungphu Oct 11 '18 at 05:46
  • Possible duplicate of [How to overcome "datetime.datetime not JSON serializable"?](https://stackoverflow.com/questions/11875770/how-to-overcome-datetime-datetime-not-json-serializable) – kungphu Oct 11 '18 at 05:49
  • I need date time in the above mentioned format only, so that my other application can read it from MongoDB. – Chethan kumar Oct 11 '18 at 08:12
  • I found an suitable answer here, https://stackoverflow.com/questions/41999094/how-to-insert-datetime-string-into-mongodb-as-isodate-using-pymongo/41999637 – Chethan kumar Oct 16 '18 at 12:44

0 Answers0