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

后端 未结 8 2193
醉话见心
醉话见心 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 19:56

    You can also look into pandas.Timestamp, which includes methods like .now and .today. Unlike pandas.to_datetime('now'), pandas.Timestamp.now() won't default to UTC:

      import pandas as pd
    
      pd.Timestamp.now() # will return California time
      # Timestamp('2018-12-19 09:17:07.693648')
    
      pd.to_datetime('now') # will return UTC time
      # Timestamp('2018-12-19 17:17:08')
    
    0 讨论(0)
  • 2020-12-23 19:58

    Using pandas: pd.Timestamp("today").strftime("%m/%d/%Y")

    0 讨论(0)
  • 2020-12-23 20:02
    import datetime
    def today_date():
        '''
        utils:
        get the datetime of today
        '''
        date=datetime.datetime.now().date()
        date=pd.to_datetime(date)
        return date
    Df['Date'] = today_date()
    

    this could be safely used in pandas dataframes.

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

    pd.datetime.now().strftime("%d/%m/%Y")

    this will give output as '11/02/2019'

    you can use add time if you want

    pd.datetime.now().strftime("%d/%m/%Y %I:%M:%S")

    this will give output as '11/02/2019 11:08:26'

    strftime formats

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

    Easy solution in Python3+:

    import time
    
    
    todaysdate = time.strftime("%d/%m/%Y")
    
    #with '.' isntead of '/'
    todaysdate = time.strftime("%d.%m.%Y")
    
    0 讨论(0)
  • 2020-12-23 20:13

    i got the same problem so tried so many things but finally this is the solution.

    import time 
    
    print (time.strftime("%d/%m/%Y"))
    
    0 讨论(0)
提交回复
热议问题