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
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')
Using pandas: pd.Timestamp("today").strftime("%m/%d/%Y")
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.
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
Easy solution in Python3+:
import time
todaysdate = time.strftime("%d/%m/%Y")
#with '.' isntead of '/'
todaysdate = time.strftime("%d.%m.%Y")
i got the same problem so tried so many things but finally this is the solution.
import time
print (time.strftime("%d/%m/%Y"))