Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting year from a date string in python

The date in my data does not come in one single format. It comes as "yyyy", "mm/dd/yyyy" and "Jun-03". I want to extract the year out of it.

like image 996
mickey Avatar asked Sep 26 '26 17:09

mickey


1 Answers

Using dateutil:

In [24]: import dateutil.parser as parser

In [28]: parser.parse('Jun-03').year  # assumes the current year
Out[28]: 2013

In [29]: parser.parse('08/09/2012').year
Out[29]: 2012

In [30]: parser.parse('2012').year
Out[30]: 2012
like image 133
unutbu Avatar answered Sep 30 '26 06:09

unutbu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!