datetime is a standard library often used by python to get the current time and date
The datetime is a module, and the datetime module also contains a datetime class with the same name. The datetime class is imported through from datetime import datetime;
if only import datetime is imported, the full name datetime.datetime must be used when quoting.
datetime.now() returns the current date and time of the datetime type
curr_date = datetime.datetime.now()
output result:
Use the strftime() method to format the date format:
curr_date1 = datetime.datetime.now().strftime('%Y-%m-%d')
output result:
curr_date2 = datetime.datetime.now().strftime('%Y%m%d')
output result:
In addition, you can also get the date before and after the current date
# 往前10天
befo_date_10 = (curr_date + datetime.timedelta(days=-10)).strftime('%Y-%m-%d')
# 往后5天
after_date_5 = (curr_date + datetime.timedelta(days=+5)).strftime('%Y-%m-%d')
Output result:
Finally, share a low-level python basic tutorial error:
Due to the datetime.py file in the current folder, an error was reported when running the c# tutorial : module'datetime' has no attribute'now'
Solution:
Modify the name of the py file to something else, note: don't name it with the vb.net tutorial
key word!