A Python Trick every Day (4)
Hello everyone, this is Milo, a blogger who wants to share with you the techniques, interviews and growth experiences related to test development!
Welcome everyone to pay attention to my public number: test development pit goods.
Many of you have used the Datetime library and can write datetime.now() and the corresponding formatting code without any problems:
from datetime import datetime
print(datetime.now())
print(datetime.strftime("%Y-%m-%d %H:%M:%S"))
Copy the code
But there are other uses for datetime that you may not know about.
The story
When we do statistics, we will encounter such a scenario. For example, to get the number of test cases executed so far this month, we need to get a string like 2021-07-01 00:00:00.
Or rather, I once wrote a question like this:
Gets the date string for the first day of the month. If today is April 10, 2021, output “2021-04-01 00:00:00”.
Think about it for a second. What would you do? I don’t know if everyone has the skills, but I’ll try my best to write the best possible solution.
- Solution 1 (awkward type)
from datetime import datetime
result = datetime.now().strftime("%Y-%m-%d")
day = result.split("") [0].split("-") [2]
result = result.replace(day, "01") + "00:00:00"
print(result)
Copy the code
This method is to solve the problem from the point of view of the string. It is simple and honest. It is my original appearance.
- Method 2
from datetime import datetime
now = datetime.now()
now = now.replace(now.year, now.month, 1.0.0.0)
result = now.strftime("%Y-%m-%d %H:%M:%S")
print(result)
Copy the code
The time can be set, I set the day to 1, minutes and seconds to 0, and then the formatting is successful.
-
Method three
But actually, because you don’t know this, so the end of the show, when the date formatting can be specified.
from datetime import datetime
print(datetime.now().strftime("%Y-%m-01 00:00:00"))
Copy the code
There are a lot of ways to do datetime, but if you want to manipulate a date, you don’t want to use strings. You want to use the datetime method, which is simple and quick.
The advanced
If you want to add or subtract the time, for example, I want to get today 30 days later, or calculate how long has passed since the college entrance examination, you can use datetime to solve this problem.
If I joined the company on 6.1 and I want to see how long I’ve been there, I could write:
from datetime import timedelta, datetime
come = datetime.strptime("2021-06-01"."%Y-%m-%d")
# Calculate the date difference to get the number of days since you joined the company
days = (datetime.now() - come).days
print(days)
# Calculate the time after 3 months to work out the date of conversion
engineer = come + timedelta(days=90)
print(engineer.strftime("%Y-%m-%d"))
Copy the code
As you can see, dateTime can be added or subtracted, and the object after addition or subtraction is a TimeDelta object, which is why I stressed not to use strings.
A small survey
If you think the skills are useful and learned, please please click a like, like more I will continue to update, like less I will not update similar.