python 示例_带有示例的Python time replace()方法

python 示例

Python time.replace()方法 (Python time.replace() Method)

time.replace() method is used to manipulate objects of time class of module datetime.

time.replace()方法用于操作datetime模块的时间类的对象。

It is used to replace the time with the same value, except for those parameters given new values by whichever keyword arguments are specified in the brackets. It is an instance method which means that it works on an instance of the class.

它用于用相同的值替换时间,但括号中指定的关键字参数给定新值的参数除外。 这是一个实例方法,这意味着它可以在类的实例上工作。

Module:

模块:

    import datetime

Class:

类:

    from datetime import time

Syntax:

句法:

    replace(
        hour=self.hour, 
        minute=self.minute, 
        second=self.second, 
        microsecond=self.microsecond, 
        tzinfo=self.tzinfo, 
        * fold=0
        )

Parameter(s):

参数:

  • hour: in range(24)

    小时 :在范围内(24)

  • minute: in range(60)

    分钟 :在范围内(60)

  • second: in range(60)

    秒 :范围(60)

  • microsecond: in range(1000000)

    微秒 :范围(1000000)

  • tzinfo: object passed as the tzinfo argument to the datetime constructor, or None if none was passed

    tzinfo :作为tzinfo参数传递给datetime构造函数的对象;如果未传递,则返回None

  • fold: [0,1]

    倍数 :[0,1]

If values are not in the given range a ValueError is raised.

如果值不在给定范围内,则会引发ValueError

Return value:

返回值:

The return type of this method is a time class object after replacing the parameters.

该方法的返回类型是替换参数后的时间类对象。

Example:

例:

## 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()

Output

输出量

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


翻译自: https://www.includehelp.com/python/time-replace-method-with-example.aspx

python 示例

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值