How to set a variable to be “Today's” date in Python/Pandas

后端 未结 8 2194
醉话见心
醉话见心 2020-12-23 19:35

I am trying to set a variable to equal today\'s date.

I looked this up and found a related article:

Set today date as default value in the model

Howe

相关标签:
8条回答
  • 2020-12-23 20:16

    You mention you are using Pandas (in your title). If so, there is no need to use an external library, you can just use to_datetime

    >>> pandas.to_datetime('today').normalize()
    Timestamp('2015-10-14 00:00:00')
    

    This will always return today's date at midnight, irrespective of the actual time, and can be directly used in pandas to do comparisons etc. Pandas always includes 00:00:00 in its datetimes.

    Replacing today with now would give you the date in UTC instead of local time; note that in neither case is the tzinfo (timezone) added.

    In pandas versions prior to 0.23.x, normalize may not have been necessary to remove the non-midnight timestamp.

    0 讨论(0)
  • 2020-12-23 20:16

    If you want a string mm/dd/yyyy instead of the datetime object, you can use strftime (string format time):

    >>> dt.datetime.today().strftime("%m/%d/%Y")
                       # ^ note parentheses
    '02/12/2014'
    
    0 讨论(0)
提交回复
热议问题