What's the simplest way to subtract a month from a date in Python?

前端 未结 21 1935
[愿得一人]
[愿得一人] 2020-12-02 09:12

If only timedelta had a month argument in it\'s constructor. So what\'s the simplest way to do this?

EDIT: I wasn\'t thinking too hard about this as was poin

21条回答
  •  悲&欢浪女
    2020-12-02 09:40

    Simplest Way that i have tried Just now

    from datetime import datetime
    from django.utils import timezone
    
    
    
    
    
    current = timezone.now()
    if current.month == 1:
         month = 12
    else:
         month = current.month - 1
    current = datetime(current.year, month, current.day)
    

提交回复
热议问题