Get (year,month) for the last X months

后端 未结 7 698
死守一世寂寞
死守一世寂寞 2020-12-10 05:06

I got a very simple thing to to in python: I need a list of tuples (year,month) for the last x months starting (and including) from today. So, for x=10 and toda

相关标签:
7条回答
  • 2020-12-10 05:46

    I don't see it documented anywhere, but time.mktime will "roll over" into the correct year when given out-of-range, including negative, month values:

    x = 10
    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)])
    
    0 讨论(0)
提交回复
热议问题