How to get current time in python and break up into year, month, day, hour, minute?

  Kiến thức lập trình

I would like to get the current time in Python and assign them into variables like year, month, day, hour, minute. How can this be done in Python 2.7?


The datetime module is your friend:

import datetime
now =
print(now.year, now.month,, now.hour, now.minute, now.second)
# 2015 5 6 8 53 40

You don’t need separate variables, the attributes on the returned datetime object have all you need.


Here’s a one-liner that comes in just under the 80 char line max.

import time
yr, month, day, hr, minute = map(int, time.strftime("%Y %m %d %H %M").split())


The datetime answer by tzaman is much cleaner, but you can do it with the original python time module:

import time
strings = time.strftime("%Y,%m,%d,%H,%M,%S")
t = strings.split(',')
numbers = [ int(x) for x in t ]
print numbers


[2016, 3, 11, 8, 29, 47]

By unpacking timetuple of datetime object, you should get what you want:

from datetime import datetime

n =
t = n.timetuple()
y, m, d, h, min, sec, wd, yd, i = t


Let’s see how to get and print day,month,year in python from current time:

import datetime

now =
year = '{:02d}'.format(now.year)
month = '{:02d}'.format(now.month)
day = '{:02d}'.format(
hour = '{:02d}'.format(now.hour)
minute = '{:02d}'.format(now.minute)
day_month_year = '{}-{}-{}'.format(year, month, day)

print('day_month_year: ' + day_month_year)


day_month_year: 2019-03-26

For python 3

import datetime
now =
print(now.year, now.month,, now.hour, now.minute, now.second)

import time
year = time.strftime("%Y") # or "%y"

You can use gmtime

from time import gmtime

detailed_time = gmtime() 
#returns a struct_time object for current time

year = detailed_time.tm_year
month = detailed_time.tm_mon
day = detailed_time.tm_mday
hour = detailed_time.tm_hour
minute = detailed_time.tm_min

Note: A time stamp can be passed to gmtime, default is current time as
returned by time()


See struct_time

Three libraries for accessing and manipulating dates and times, namely datetime, arrow and pendulum, all make these items available in namedtuples whose elements are accessible either by name or index. Moreover, the items are accessible in precisely the same way. (I suppose if I were more intelligent I wouldn’t be surprised.)

>>> import datetime
>>> import arrow
>>> import pendulum
>>> [[i] for i in [YEARS, MONTHS, DAYS, HOURS, MINUTES]]
[2017, 6, 16, 19, 15]
>>> [[i] for i in [YEARS, MONTHS, DAYS, HOURS, MINUTES]]
[2017, 6, 16, 19, 15]
>>> [[i] for i in [YEARS, MONTHS, DAYS, HOURS, MINUTES]]
[2017, 6, 16, 19, 16]

This is an older question, but I came up with a solution I thought others might like.

def get_current_datetime_as_dict():
n =
t = n.timetuple()
field_names = ["year",
return dict(zip(field_names, t))

timetuple() can be zipped with another array, which creates labeled tuples. Cast that to a dictionary and the resultant product can be consumed with get_current_datetime_as_dict()['year'].

This has a little more overhead than some of the other solutions on here, but I’ve found it’s so nice to be able to access named values for clartiy’s sake in the code.