带有示例的Python datetime timetz()方法

Python datetime.timetz()方法 (Python datetime.timetz() Method)

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

datetime.timetz()方法用于操作模块datetime的datetime类的对象。

It uses an instance method and converts it and returns a time object with the same hour, minute, second, microsecond, and fold and tzinfo attributes. Unlike datetime.timetz() method the tzinfo is not None here if present in the original datetime instance object.

它使用实例方法进行转换,并返回具有相同小时,分钟,秒,微秒以及fold和tzinfo属性的时间对象。 与datetime.timetz()方法不同,如果原始日期时间实例对象中存在tzinfo,则这里的tzinfo不是None。

Module:

模块:

    import datetime

Class:

类:

    from datetime import datetime

Syntax:

句法:

    timetz()

Parameter(s):

参数:

  • None

    没有

Return value:

返回值:

The return type of this method is a time object with same hour, minute, second, microsecond, fold and tzinfo.

此方法的返回类型是具有相同时,分,秒,微秒,倍数和tzinfo的时间对象。

Example:

例:

## Creating a time object from a datetime object
import datetime
## Creating datetime instance
x = datetime.datetime(2020, 3, 4, 23, 12, 23, 44)
d = x.timetz()
print("Original object:", x)
print("New time object:",d)
print()

x = datetime.datetime.now()
d = x.timetz()
print("Original date and time", x)
print("New time object:", d)
print()

t = datetime.timedelta(hours=5, minutes= 30)
tzobj = datetime.timezone(t, name="IST")
x = datetime.datetime(2020, 3, 22, 12, 23, 34, 45, tzobj)
d = x.timetz()
print("Original datetime object:", x)
print("Time object with tzinfo attributes:", d)
print("Time object without tzinfo attributes:", x.time())

Output

输出量

Original object: 2020-03-04 23:12:23.000044
New time object: 23:12:23.000044

Original date and time 2020-05-03 18:26:35.437067
New time object: 18:26:35.437067

Original datetime object: 2020-03-22 12:23:34.000045+05:30
Time object with tzinfo attributes: 12:23:34.000045+05:30
Time object without tzinfo attributes: 12:23:34.000045


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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值