If I have lets say this string "2008-12-12 19:21:10" how can I convert it into a date and get the year, month and day from that created object separately?
asked Sep 14, 2012 at 18:49
2
Use the datetime.datetime.strptime[]
function:
from datetime import datetime
dt = datetime.strptime[datestring, '%Y-%m-%d %H:%M:%S']
Now you have a datetime.datetime
object, and it has .year
, .month
and .day
attributes:
>>> from datetime import datetime
>>> datestring = "2008-12-12 19:21:10"
>>> dt = datetime.strptime[datestring, '%Y-%m-%d %H:%M:%S']
>>> print dt.year, dt.month, dt.day
2008 12 12
answered Sep 14, 2012 at 18:50
Martijn Pieters♦Martijn Pieters
987k274 gold badges3883 silver badges3242 bronze badges
0
//www.tutorialspoint.com/python/time_strptime.htm Here you can find strptime[] method complete description. where you can find all type of strings. Eg:- To convert string like this '15-MAY-12'
>>>from datetime import datetime
>>>datestring = "15-MAY-12"
>>>dt = datetime.strptime[datestring, '%d-%b-%Y']
>>>print[dt.year, dt.month, dt.day]
2012 MAY 15
d_kennetz
5,0285 gold badges21 silver badges44 bronze badges
answered Jun 26, 2017 at 18:36
1
with milliseconds
>>> from datetime import datetime
>>> datestring = "2018-04-11 23:36:18.886585"
>>> dt = datetime.strptime[datestring, '%Y-%m-%d %H:%M:%S.%f']
>>> print dt.year, dt.month, dt.day
2018 04 11
answered Apr 12, 2018 at 6:40
One thing to add; pay attention that %y
is for two digit
year notation %Y
is for the four digit one:
import datetime
datestring = '15-MAY-12'
print[datetime.datetime.strptime[datestring, '%d-%b-%y']]
>>> datetime.datetime[2012, 5, 15, 0, 0]
datestring = '15-MAY-2012'
print[datetime.datetime.strptime[datestring, '%d-%b-%Y']]
>>> datetime.datetime[2012, 5, 15, 0, 0]
answered Jun 20, 2018 at 21:51
ShividShivid
1,1851 gold badge20 silver badges35 bronze badges
- Home
- Python How Tos
- Get year from a Date in Python
In this article, we will learn how to get the year from a given date in Python. We will use the built-in module available for getting date and time information and some custom codes as well to see them working. Let's first have a quick look over what are dates in Python.
Python Date
In Python, we can work on Date functions by importing a built-in module datetime
available in Python. We have date objects to work with dates. This datetime module contains dates in the form of year, month, day, hour, minute, second, and microsecond. The datetime module has many methods to
return information about the date object. It requires date, month, and year values to compute the function. Date and time functions are compared like mathematical expressions between various numbers.
There are various ways in which we can get the year. We can get a year from a given input date, current date using date object. We will import date
class from datetime
module and will print the output. Look at the examples below.
Example: Get Year from the given Date
In this
example, we import datetime module. Using datetime
object, we applied today[]
function to extract the current date and then year[]
to get only the year from the current date.
import datetime
year = datetime.datetime.today[].year
print[year]
2021
Example: Get Year from the given Date Using now[] Function
In this example, we import datetime module. Using datetime
object, we applied now[]
function to extract the current date first and then year[]
to get only the year from the current date.
import datetime
year = datetime.datetime.now[].year
print[year]
2021
Conclusion
In this article, we learned to get the year from the current date using two datetime
functions. We used now[].year[]
and today[].year[]
to extract the year from the current date. We saw two examples of the implementation of these two functions.