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

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

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

It uses an instance of the class and returns a datetime object with new tzinfo attribute tz. It works on a datetime class instance.

它使用该类的实例,并返回具有新tzinfo属性tzdatetime对象。 它适用于datetime类实例。

Module:

模块:

    import datetime

Class:

类:

    from datetime import datetime

Syntax:

句法:

    astimezone(tz)

Parameter(s):

参数:

  • tz - timezone information which should be changed.

    tz-应更改的时区信息。

Return value:

返回值:

Returns a datetime object with new tzinfo attribute tz.

返回具有新的tzinfo属性tz的日期时间对象。

Example:

例:

from datetime import datetime
import pytz
naive= datetime.now()
## Tzinfo is missing from the time object
## which is naive
print("Tzinfo:",naive.tzinfo)
print()
## Adding a timezone
timezone = pytz.timezone("Asia/Kolkata")
aware1 = naive.astimezone(timezone)
print("Tzinfo:",aware1.tzinfo)
print()
## After adding the timezone info,
## the object it becomes aware
timezone = pytz.timezone("Asia/Tokyo")
aware2 = aware1.astimezone(timezone)
print("Initial tzinfo:",aware1.tzinfo)
print("Final tzinfo:",aware2.tzinfo)
print()
## No timezone was defined for naive
print(naive)
## Timezone changed from None to Asia/Kolkata
## for aware1 object
print(aware1)
## Timezone changed from Asia/Kolkata to
## Asia/Tokyo for aware2 object
print(aware2)

Output

输出量

Tzinfo: None
Tzinfo: Asia/Kolkata
Initial tzinfo: Asia/Kolkata
Final tzinfo: Asia/Tokyo
2020-04-30 18:44:12.784666
2020-05-01 00:14:12.784666+05:30
2020-05-01 03:44:12.784666+09:00

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

Python datetime astimezone()方法与示例相关推荐

  1. python forward(10)什么意思-Python turtle.forward方法代码示例

    本文整理汇总了Python中turtle.forward方法的典型用法代码示例.如果您正苦于以下问题:Python turtle.forward方法的具体用法?Python turtle.forwar ...

  2. Python cv2.minMaxLoc方法代码示例

    本文整理汇总了Python中cv2.minMaxLoc方法的典型用法代码示例.如果您正苦于以下问题:Python cv2.minMaxLoc方法的具体用法?Python cv2.minMaxLoc怎么 ...

  3. msvcrt python linux,Python msvcrt.CrtSetReportMode方法代码示例

    # 需要导入模块: import msvcrt [as 别名] # 或者: from msvcrt import CrtSetReportMode [as 别名] def __enter__(self ...

  4. archlm检验python,Python sublime.arch方法代码示例

    # 需要导入模块: import sublime [as 别名] # 或者: from sublime import arch [as 别名] def run(self): info = {} inf ...

  5. python datetime datetime_Python datetime.tzinfo方法代碼示例

    本文整理匯總了Python中datetime.datetime.tzinfo方法的典型用法代碼示例.如果您正苦於以下問題:Python datetime.tzinfo方法的具體用法?Python da ...

  6. Python datetime isocalendar()方法与示例

    Python datetime.isocalendar()方法 (Python datetime.isocalendar() Method) datetime.isocalendar() method ...

  7. 带有示例的Python datetime weekday()方法

    Python datetime.weekday()方法 (Python datetime.weekday() Method) datetime.weekday() method is used to ...

  8. python的datetime举例_Python datetime模块的使用示例

    1.获取当前年月日时分秒 # -*- encoding=utf-8 -*- import datetime now = datetime.datetime.now() print("now: ...

  9. c# datetime._C#| DateTime.AddTicks()方法与示例

    c# datetime. DateTime.AddTicks()方法 (DateTime.AddTicks() Method) DateTime.AddTicks() method is used t ...

最新文章

  1. linux添加到普通用户sudo才干
  2. mysql更新数据索引慢_mysql添加索引,查询反而变慢
  3. java学习(163):同步方法锁
  4. 《Java8实战》笔记(16):结论以及Java的未来
  5. 李航老师亲自推荐的《统计学习方法》课件下载
  6. PyTorch | torch.full()使用方法 | torch.full()如何使用? torch.full()例子说明 | 通过torch.full创建全相同的张量
  7. 【Antlr】 Antlr 错误与恢复
  8. portal认证 php,如何用PHP制作OSSH 免费版华为Portal系统认证前端页面
  9. 她看:2021年95后女性人群洞察与媒体消费趋势前瞻.pdf(附下载链接)
  10. Object-C,NSSet,不可变集合
  11. linux中提示没有unzip命令解决方法
  12. python做鼠标自动移动_Python实现鼠标自动在屏幕上随机移动功能
  13. html大作业网页代码 web网页设计实例作业 ——二手书店-大学生书店(13页) 学生个人网站作业模板 简单个人网页制作
  14. python毕业设计总结范文大全_毕业设计心得体会范文3篇
  15. split 自动分割文件脚本
  16. 网易租赁服务器怎么添加组件,《我的世界》租赁服添加MOD教程 租赁服怎么添加小精灵MOD?...
  17. 漫威联手六位中国运动员致敬漫威电影宇宙十年
  18. 【树莓派】一、新坑入门-购买篇、基础配置篇
  19. Flutter-Container详解
  20. 【Python第一课】课程介绍

热门文章

  1. php 做更新进度条,PHP exec()后更新Bootstrap进度条
  2. 跨域产生的原因和解决方法_板式家具开料机加工过程产生崩边原因及解决方法...
  3. python句柄无效_subprocess.Popen 运行windows命令出现“句柄无效”报错的解决方法
  4. window.open不重复打开同一个名称的窗口_干货满满|Ctrl键的正确打开方式
  5. Java并发篇_线程详解
  6. git 命令 clone分支的代码
  7. Find the Difference(leetcode389)
  8. react 16.6 懒加载 Lazy 尝鲜
  9. ftp上传和下载命令
  10. Activity和Service交互