Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the previous month

Tags:

python

I've seen some methods using dateutil module to do this, but is there a way to do this without just using the built in libs?

For example, the current month right now is July. I can do this using the datetime.now() function.

What would be the easiest way for python to return the previous month?

like image 225
dyao Avatar asked Jan 07 '23 21:01

dyao


1 Answers

It's very easy:

>>> previous_month = datetime.now().month - 1
>>> if previous_month == 0:
...     previous_month = 12
like image 140
Lennart Regebro Avatar answered Jan 10 '23 10:01

Lennart Regebro