-1

I need to convert a set of strings that is in this format "2017-12-02 23:55:66.333+01:00" to datetime.

I tried using this:

from datetime import datetime

date_time_str = '2017-12-02 23:55:66.333+01:00'

date_time_obj = datetime.strptime(date_time_str, '%Y-%m-%d %H:%M:%S%z')

print ("The date is", date_time_obj)

But i have this output:

>> ValueError: raise ValueError("time data %r does not match format %r" %
ValueError: time data '2017-12-02 23:55:66.333+01:00' does not match format '%Y-%m-%d %H:%M:%S%z'

How can i convert this date or this date_time_str is not valid?

Achref MH
  • 31
  • 1
  • 5
  • 3
    How does a minute have a 66th second? Where are the milliseconds? – jonrsharpe Jan 12 '22 at 00:00
  • I have a file csv with this data : "2017-12-02 23:55:66.333+01:00", so i need to add a raise:except, the seconds are greater than 60 – Achref MH Jan 12 '22 at 00:33
  • Does this answer your question? [How do I parse an ISO 8601-formatted date?](https://stackoverflow.com/questions/127803/how-do-i-parse-an-iso-8601-formatted-date) – FObersteiner Jan 12 '22 at 06:31

2 Answers2

1

here is what you want to do, missing stripping micorosecond and timezone section :

date_time_str = '2017-12-02 23:55:59.333+01:00'
date_time_obj = datetime.strptime(date_time_str, '%Y-%m-%d %H:%M:%S.%f%z')
print ("The date is", date_time_obj)

output :

The date is 2017-12-02 23:55:59.333000+01:00

or simply using this in python 3.7+:

datetime.fromisoformat(date_time_str)
eshirvana
  • 23,227
  • 3
  • 22
  • 38
  • 1
    In Python 3.7 onwards, there's also `datetime.fromisoformat(date_time_str)`. – zmike Jan 12 '22 at 00:04
  • @zmike yes , there is. thank you for mentioning – eshirvana Jan 12 '22 at 00:05
  • @eshirvana, thank you! i have this string '2017-12-02 23:55:66.333+01:00' csv file, then i need to raise an exception because of seconds are greater than 60, right? – Achref MH Jan 12 '22 at 00:12
  • @AchrefMH yes , otherwise it is not a valid datetime value and actually Its more than 59 , 60 is also not acceptable – eshirvana Jan 12 '22 at 00:13
1

There are two problems:

  • your seconds are greater than 60
  • you need to include the microseconds formatter

Thus to fix it,

from datetime import datetime
date_time_str = '2017-12-02 23:55:06.333+01:00'
date_time_obj = datetime.strptime(date_time_str, '%Y-%m-%d %H:%M:%S.%f%z')
print("The date is", date_time_obj)

yields

The date is 2017-12-02 23:55:06.333000+01:00

You can catch the error caused by the seconds being too large and raise a helpful error message by

from datetime import datetime
date_time_str = '2017-12-02 23:55:06.333+01:00'
try:
    date_time_obj = datetime.strptime(date_time_str, '%Y-%m-%d %H:%M:%S.%f%z')
except ValueError as ve:
    if "unconverted data remains" in ve.args[0]:
        raise ValueError("One of the values in the time string isn't meaninful")
    else:
        raise ve
print("The date is", date_time_obj)
lumalot
  • 141
  • 10
  • Thanks a lot, as I mentioned above , i have this string '2017-12-02 23:55:66.333+01:00' csv file, then i need to raise an exception because of the seconds are greater than 60, right? – Achref MH Jan 12 '22 at 00:13
  • Python will raise an error, but depending on your userbase, you may want to do a try:except and raise your own, more explanatory error – lumalot Jan 12 '22 at 00:14
  • yes i will do a try:except. thanks again :) – Achref MH Jan 12 '22 at 00:30