This is the sixth day of my participation in the August More text Challenge. For details, see: August More Text Challenge
Time offset is the time that is added or subtracted from the specified time by a period of time
There are two main ways to do this in Python: with timeDelta, and with the date offset in Pandas
1 timedelta
1.1 The unit of time offset is week
1.1.1 One week later
date = datetime(2007, 5, 12, 18, 53, 32)
print(date + timedelta(weeks=1))
Copy the code
result:
The 2007-05-19 18:53:32Copy the code
1.1.2 Move forward one week
date = datetime(2007, 5, 12, 18, 53, 32)
print(date - timedelta(weeks=1))
Copy the code
result:
The 2007-05-05 18:53:32Copy the code
1.2 Time offset The unit is day
1.2.1 One day later
from datetime import timedelta, datetime
date = datetime(2007, 5, 12, 18, 53, 32)
print(date + timedelta(days=1))
Copy the code
result:
The 2007-05-13 18:53:32Copy the code
1.2.2 One day forward
date = datetime(2007, 5, 12, 18, 53, 32)
print(date - timedelta(days=1))
Copy the code
result:
The 2007-05-11 18:53:32Copy the code
1.3 Time offset The unit is hour
1.3.1 One hour later
date = datetime(2007, 5, 12, 18, 53, 32)
print(date + timedelta(hours=1))
Copy the code
result:
The 2007-05-12 19:53:32Copy the code
1.3.2 Move forward one hour
date = datetime(2007, 5, 12, 18, 53, 32)
print(date - timedelta(hours=1))
Copy the code
result:
The 2007-05-12 17:53:32Copy the code
1.4 Time offset The unit is minute
1.4.1 One minute later
date = datetime(2007, 5, 12, 18, 53, 32)
print(date + timedelta(minutes=1))
Copy the code
result:
The 2007-05-12 18:54:32Copy the code
1.4.2 Push forward one minute
date = datetime(2007, 5, 12, 18, 53, 32)
print(date - timedelta(minutes=1))
Copy the code
result:
The 2007-05-12 18:52:32Copy the code
1.5 Time offset The unit is second
1.5.1 Push back one second
date = datetime(2007, 5, 12, 18, 53, 32)
print(date + timedelta(seconds=1))
Copy the code
result:
The 2007-05-12 18:53:33Copy the code
1.5.2 Push forward one second
date = datetime(2007, 5, 12, 18, 53, 32)
print(date - timedelta(seconds=1))
Copy the code
result:
The 2007-05-12 18:53:31Copy the code
1.6 Time Offset The unit is millisecond
1.6.1 Push back 1 millisecond
date = datetime(2007, 5, 12, 18, 53, 32, 987)
print(date + timedelta(milliseconds=1))
Copy the code
result:
The 2007-05-12 18:53:32. 001987Copy the code
1.6.2 Advance one millisecond
date = datetime(2007, 5, 12, 18, 53, 32, 987)
print(date - timedelta(milliseconds=1))
Copy the code
result:
The 2007-05-12 18:53:31. 999987Copy the code
1.7 Time Offset The unit is microsecond
1.7.1 Push back one microsecond
date = datetime(2007, 5, 12, 18, 53, 32, 987)
print(date + timedelta(microseconds=1))
Copy the code
result:
The 2007-05-12 18:53:32. 000988Copy the code
1.7.2 Push forward by 1 microsecond
date = datetime(2007, 5, 12, 18, 53, 32, 987)
print(date - timedelta(microseconds=1))
Copy the code
result:
The 2007-05-12 18:53:32. 000986Copy the code
2 date offset
from datetime import datetime
from pandas.tseries.offsets import Day
date = datetime(2007, 5, 12, 18, 53, 32, 987)
print(date + Day(1))
Copy the code
result:
The 2007-05-13 18:53:32. 000987Copy the code
2.1 Time offset The unit is day
2.1.1 One day later
date = datetime(2007, 5, 12, 18, 53, 32)
print(date + Day(1))
Copy the code
result:
The 2007-05-13 18:53:32Copy the code
2.1.2 Advance by one day
date = datetime(2007, 5, 12, 18, 53, 32,)
print(date - Day(1))
Copy the code
result:
The 2007-05-11 18:53:32Copy the code
Other time units are similar to timedelta. If the units are Week, Hour, Minute, or Second, replace Day with Week, Hour, Minute, or Second. Here is not a list.