Datetime: date time module, which provides multiple methods to operate date and time
Strftime: format date and time
Get today’s date, yesterday’s date, formatted date
>>> import datetime
>>> today=datetime.date.today()
>>> print today
2018-01-17
>>> formatted_today=today.strftime('%y%m%d')
>>> print formatted_today
180117
>>> yesterday=int(formatted_today)-1
>>> print yesterday
180116
The above content was written on January 17, 2018. Now some problems are found on March 1. When it crosses months, the above code will appear.
yesterday=int(formatted_today)-1
After executing this line of code, the day before March 1 becomes March 0
How to change it?
yesterday = (datetime.date.today() + datetime.timedelta(days=-1)).strftime('%Y%m%d')
Get today’s date first
Then use the timedetla object of datetime, which represents the difference between the two times, datetime.timedelta (days = – 1) means the time of the day before. The day before March 1 is February 28.
Finally, we use strftime to transform the time format
Read More:
- Linux view current date and time
- How to get the current time in java time string
- [BUG-mybatis-plus] org.postgresql.util.PSQLException: ERROR: syntax error at or near “current_date“
- The python version output from the command line is inconsistent with the python version in the current CONDA environment
- 1、 Java 8 date and local date
- Local date time conversion in java8
- Python opencv (3) get image size
- Get picture captcha with Python + Chrome
- ERROR 2026 (HY000): SSL connection error: ASN: before date in the future
- ‘Conversion failed when converting date and/or time from character string.DB-Lib error message 241,
- Python uses the priority queue to get the maximum k elements
- In SQLite database, set the default value for the field as the current time
- Perl application – delete “. SVN” folder in current directory and subdirectory
- java.text.ParseException: Unparseable date: “2021-06-24“
- Set the default time to the current value in MySQL
- Mutual conversion between date and localdatetime
- Python – get the information of calling function from called function
- [JS] use date. Now(). Tostring() to generate serial number
- Ecarts comes with the date formatting method formattime
- Solve the problem of unable to locate package python3.6 when upgrading from python3.5 to python3.6 in ubantu16.04