📜  Python DateTime – time.replace() 方法和示例

📅  最后修改于: 2022-05-13 01:55:44.387000             🧑  作者: Mango

Python DateTime – time.replace() 方法和示例

在本文中,我们将讨论Python的 time.replace() 方法。该方法用于操作模块 datetime 的时间类对象。它用于用相同的值替换时间,除了那些由任何关键字参数赋予新值的参数。

获取时间和显示的Python程序:



Python3
# import time from datetime
from datetime import time
  
# create time
x = time(5,34,7,6789)
print("Time:", x)


Python3
# import time from datetime
from datetime import time
  
# create time
x = time(5, 34, 7, 6789)
  
print("Actual Time:", x)
  
# replace hour from 5 to 10
final = x.replace(hour = 10)
  
print("New time after changing the hour:", final)


Python3
# import time from datetime
from datetime import time
  
# create time
x = time(5, 34, 7, 6789)
  
print("Actual Time:", x)
  
# replace minute from 34 to 12
final = x.replace(minute = 12)
  
print("New time after changing the minute:", final)


Python3
# import time from datetime
from datetime import time
  
# create time
x = time(5,34,7,6789)
  
print("Actual Time:", x)
  
# replace second from 7 to 2
final = x.replace(second = 2)
  
print("New time after changing the second:", final)


Python3
# import time from datetime
from datetime import time
  
# create time
x = time(5,34,7,6789)
  
print("Actual Time:", x)
  
# replace hour from 5 to 10
# replace minute from 34 to 11
# replace second from 7 to 1
# replace milli second from 6789 to 1234
final = x.replace(hour = 10, minute = 11,
                  second = 1, microsecond = 1234)
  
print("New time :", final)


输出:

Time: 05:34:07.006789

示例 1:从给定时间替换小时的Python程序

蟒蛇3

# import time from datetime
from datetime import time
  
# create time
x = time(5, 34, 7, 6789)
  
print("Actual Time:", x)
  
# replace hour from 5 to 10
final = x.replace(hour = 10)
  
print("New time after changing the hour:", final)

输出:

Actual Time: 05:34:07.006789
New time after changing the hour: 10:34:07.006789

示例 2:从时间替换分钟的Python程序

蟒蛇3

# import time from datetime
from datetime import time
  
# create time
x = time(5, 34, 7, 6789)
  
print("Actual Time:", x)
  
# replace minute from 34 to 12
final = x.replace(minute = 12)
  
print("New time after changing the minute:", final)

输出:

Actual Time: 05:34:07.006789
New time after changing the minute: 05:12:07.006789

示例 3:替换秒的Python代码

蟒蛇3

# import time from datetime
from datetime import time
  
# create time
x = time(5,34,7,6789)
  
print("Actual Time:", x)
  
# replace second from 7 to 2
final = x.replace(second = 2)
  
print("New time after changing the second:", final)

输出:

Actual Time: 05:34:07.006789
New time after changing the second: 05:34:02.006789

示例 4:一次替换所有的Python程序

蟒蛇3

# import time from datetime
from datetime import time
  
# create time
x = time(5,34,7,6789)
  
print("Actual Time:", x)
  
# replace hour from 5 to 10
# replace minute from 34 to 11
# replace second from 7 to 1
# replace milli second from 6789 to 1234
final = x.replace(hour = 10, minute = 11,
                  second = 1, microsecond = 1234)
  
print("New time :", final)

输出:

Actual Time: 05:34:07.006789
New time : 10:11:01.001234