Formatting “yesterday's” date in python

后端 未结 6 2120
难免孤独
难免孤独 2020-12-02 03:56

I need to find \"yesterday\'s\" date in this format MMDDYY in Python.

So for instance, today\'s date would be represented like this: 111009

I ca

6条回答
  •  情歌与酒
    2020-12-02 04:39

    To expand on the answer given by Chris

    if you want to store the date in a variable in a specific format, this is the shortest and most effective way as far as I know

    >>> from datetime import date, timedelta                   
    >>> yesterday = (date.today() - timedelta(days=1)).strftime('%m%d%y')
    >>> yesterday
    '020817'
    

    If you want it as an integer (which can be useful)

    >>> yesterday = int((date.today() - timedelta(days=1)).strftime('%m%d%y'))
    >>> yesterday
    20817
    

提交回复
热议问题