Python time.replace() 方法
time.replace() 方法用于操作模块 datetime 的时间类对象。
它用于用相同的值替换时间,除了那些由括号中指定的关键字参数赋予新值的参数。它是一个实例方法,这意味着它适用于类的实例。
模块:
import datetime
类:
from datetime import time
用法:
replace( hour=self.hour, minute=self.minute, second=self.second, microsecond=self.microsecond, tzinfo=self.tzinfo, * fold=0 )
参数:
hour
:在范围内(24)minute
:在范围内(60)second
:在范围内(60)microsecond
:在范围内(1000000)tzinfo
:object 作为 tzinfo 参数传递给 datetime 构造函数,或者 None 如果没有传递fold
:[0,1]
如果值不在给定范围内,则会引发 ValueError。
返回值:
该方法的返回类型是替换参数后的时间类对象。
例:
## Python program explaining the
## use of replace() method
from datetime import time
## Creating an instance
x = time(4,54,23, 37777)
print("Time entered was:", x)
print()
## Using replace() method
d = x.replace(hour = 22)
print("New time after changing the hour:", d)
print()
d = x.replace(minute=10)
print("New time after changing the minute:", d)
print()
d = x.replace(second=30)
print("Time after changing the second:", d)
print()
d = x.replace(hour=20, minute=30)
print("Time after changing hour and minute:", d)
print()
d = x.replace(hour= 19, minute =12, second=34, microsecond=3333)
print("Time after changes:", d)
print()
输出
Time entered was:04:54:23.037777 New time after changing the hour:22:54:23.037777 New time after changing the minute:04:10:23.037777 Time after changing the second:04:54:30.037777 Time after changing hour and minute:20:30:23.037777 Time after changes:19:12:34.003333
相关用法
- Python time.monotonic()用法及代码示例
- Python time.asctime()用法及代码示例
- Python time.clock()用法及代码示例
- Python time.ctime()用法及代码示例
- Python time.perf_counter()用法及代码示例
- Python time.get_clock_info()用法及代码示例
- Python time.gmtime()用法及代码示例
- Python time.time()用法及代码示例
- Python time.mktime()用法及代码示例
- Python time.sleep()用法及代码示例
- Python time.clock_gettime_ns()用法及代码示例
- Python time.process_time_ns()用法及代码示例
- Python time.monotonic_ns()用法及代码示例
- Python time.time_ns()用法及代码示例
- Python timedelta total_seconds()用法及代码示例
- Python time.strftime()用法及代码示例
- Python time.clock_getres()用法及代码示例
- Python timeit()用法及代码示例
- Python time.clock_settime()用法及代码示例
- Python time.process_time()用法及代码示例
注:本文由纯净天空筛选整理自 Python time replace() Method with Example。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。