project mpi
project mpi

Reputation: 31

How to strip the following date time from a string?

I want to strip the date-time from the following string in Python 3:

So What I do is the following:

date_time_obj = datetime.strptime("06/30/16(Thu)12:41:05",'%d/%m/%y(%a)%H:%M:%S')

But I get a value error which means the format is not the one.

Upvotes: 1

Views: 124

Answers (2)

Kartik_Bhatnagar
Kartik_Bhatnagar

Reputation: 230

The solution to your problem is Simple! First you can have to import datetime . You can done this by

from datetime import datetime

And your format of Month and Day needs to be swapped (d and m)

date_time_obj = datetime.strptime("06/30/16(Thu)12:41:05",'%m/%d/%y(%a)%H:%M:%S')

Upvotes: 1

rudolfovic
rudolfovic

Reputation: 3276

First make sure your import is correct:

from datetime import datetime

(rather than just import datetime) Then use the correct format string:

datetime.strptime("06/30/16(Thu)12:41:05", "%m/%d/%y(%a)%H:%M:%S")

Upvotes: 1

Related Questions