0

I scraped a website and got the following Output:

2018-06-07T12:22:00+0200

2018-06-07T12:53:00+0200

2018-06-07T13:22:00+0200

Is there a way I can take the first one and convert it into a DateTime value?

  • what you mean by datetime value, just change the type or entire format ? – Vikas Periyadath Aug 01 '18 at 10:39
  • Possible duplicate of [Converting string into datetime](https://stackoverflow.com/questions/466345/converting-string-into-datetime) – Muhammad Haseeb Aug 01 '18 at 10:44
  • `import dateparser` and `dateparser.parse()` should be working for those examples. – meissner_ Aug 01 '18 at 10:44
  • There are parser functions for this purpose. GIYF: https://www.google.fr/search?client=ms-android-sonymobile&ei=8IRgW5-GOIqMlwSpkY2QAg&q=Python+datetime+from+string&oq=Python+datetime+from+string&gs_l=mobile-gws-wiz-serp.12..0l4j0i22i30.2359.24980..26245...0.0...1862.20290.4-3j4j9j5j2......0....1.......3..41j46j0i131.NCfqtVeZx8I – tangoal Aug 01 '18 at 10:45

4 Answers4

1

Just parse the string into year, month, day, hour and minute integers and then create a new date time object with those variables.

Check out the datetime docs

Spoonless
  • 561
  • 5
  • 14
0

The following function (not mine) should help you with what you want:

df['date_column'] = pd.to_datetime(df['date_column'], format = '%d/%m/%Y %H:%M').dt.strftime('%Y%V')

You can mess around with the keys next to the % symbols to achieve what you want. You may, however, need to do some light cleaning of your values before you can use them with this function, i.e. replacing 2018-06-07T12:22:00+0200 with 2018-06-07 12:22.

Laurie
  • 1,189
  • 1
  • 12
  • 28
0

You can use datetime lib.

from datetime import datetime

datetime_object = datetime.strptime('Jun 1 2005  1:33PM', '%b %d %Y %I:%M%p')

datetime.strptime documentation

Solution here

ymutlu
  • 6,585
  • 4
  • 35
  • 47
0

You can convert string format of datetime to datetime object like this using strptime, here %z is the time zone :

import datetime
dt = "2018-06-07T12:22:00+0200"
ndt = datetime.datetime.strptime(dt, "%Y-%m-%dT%H:%M:%S%z")

# output 
2018-06-07 12:22:00+02:00
Vikas Periyadath
  • 3,088
  • 1
  • 21
  • 33