python array of last n months 1

python array of last n months

x = 10 #N Months
now = time.localtime()
print([time.localtime(time.mktime((now.tm_year, now.tm_mon - n, 1, 0, 0, 0, 0, 0, 0)))[:2] for n in range(x)])

Here is what the above code is Doing:
1. time.localtime() returns a tuple of the current time.
2. time.mktime() takes a tuple of the year, month, day, hour, minute, second, day of week, day of year, and daylight savings time flag, and returns a float of the seconds since the epoch.
3. time.localtime() takes a float of the seconds since the epoch and returns a tuple of the time.
4. time.localtime()[:2] returns a tuple of the year and month.

Similar Posts