Basic operation of python--datetime module

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:
Insert picture description here

Use the strftime() method to format the date format:

curr_date1 = datetime.datetime.now().strftime('%Y-%m-%d')
output result:

Insert picture description here

curr_date2 = datetime.datetime.now().strftime('%Y%m%d')
output result:
Insert picture description here

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:

Insert picture description here

Finally, share a low-level python basic tutorial error:

Insert picture description here

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!

Guess you like

Origin blog.csdn.net/chinaherolts2008/article/details/112911920