zoukankan      html  css  js  c++  java
  • python 使用time / datetime进行时间、时间戳、日期转换

    python 使用time 进行时间、时间戳、日期格式转换

     1 #!/usr/bin/python3
     2 # -*- coding: utf-8 -*-
     3 # @Time    : 2017/11/7 15:53
     4 # @Author  : Z.C.Wang
     5 # @Email   : 
     6 # @File    : DateTime.py
     7 # @Software: PyCharm Community Edition
     8 """
     9 Description : 有关时间转换(datetime)
    10 主要内容:
    11 1) 获取当前日期和时间
    12 2) 获取指定日期和时间
    13 3) datetime转换为timestamp
    14 4) timestamp转换为datetime
    15 5) str转换为datetime
    16 6) datetime转换为str
    17 7) datetime加减
    18 """
    19 import numpy as np
    20 from datetime import datetime
    21 from datetime import timedelta
    22  
    23 # 1) 获取当前日期和时间
    24 now = datetime.now()  # 返回当前日期和时间
    25 print('1)')
    26 print('当前时间 :', now)
    27  
    28 # 2) 获取指定日期和时间
    29 dt = datetime(2017, 5, 28, 23, 10, 54)
    30 print('2)')
    31 print('指定时间 :', dt)
    32  
    33 # 3) datetime转换为timestamp
    34 dt_stamp = dt.timestamp()
    35 print('3)')
    36 print('指定时间对应时间戳 :', dt_stamp)
    37  
    38 # 4) timestamp转换为datetime
    39 t = 163423625
    40 print('4)')
    41 print('时间戳 :', t)
    42 print('对应本地时间 :', datetime.fromtimestamp(t))
    43 print('UTC标准时间 :', datetime.utcfromtimestamp(t))
    44 print('weekOfDay :', datetime.fromtimestamp(t).weekday())
    45  
    46 # 5) str转换为datetime
    47 day = datetime.strptime('2016-12-2 15:45:35', '%Y-%m-%d %H:%M:%S')
    48 print('5)')
    49 print(day)
    50  
    51 # 6) datetime转换为str
    52 now = datetime.now()
    53 print('6)')
    54 print('当前时间 :', now)
    55 print(now.strftime('%A, %B %d %H:%M, %Y'))
    56  
    57 # 7) datetime加减
    58 # datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)
    59 now = datetime.now()
    60 print('7)')
    61 print('当前时间 :', now)
    62 now_stamp = now.timestamp()
    63 print('时间戳 :', now_stamp)
    64 np.savetxt('now_stamp.txt', np.array([now_stamp]))
    65 t = now + timedelta(days=1, hours=8, minutes=5, seconds=20)
    66 print('1天8小时5分20秒之后的时间 :', t)

    python 使用datetime进行时间日期转换

     1 #!/usr/bin/python3
     2 # -*- coding: utf-8 -*-
     3 # @Time    : 2017/11/7 15:53
     4 # @Author  : Z.C.Wang
     5 # @Email   : 
     6 # @File    : DateTime.py
     7 # @Software: PyCharm Community Edition
     8 """
     9 Description : 有关时间转换(datetime)
    10 主要内容:
    11 1) 获取当前日期和时间
    12 2) 获取指定日期和时间
    13 3) datetime转换为timestamp
    14 4) timestamp转换为datetime
    15 5) str转换为datetime
    16 6) datetime转换为str
    17 7) datetime加减
    18 """
    19 import numpy as np
    20 from datetime import datetime
    21 from datetime import timedelta
    22  
    23 # 1) 获取当前日期和时间
    24 now = datetime.now()  # 返回当前日期和时间
    25 print('1)')
    26 print('当前时间 :', now)
    27  
    28 # 2) 获取指定日期和时间
    29 dt = datetime(2017, 5, 28, 23, 10, 54)
    30 print('2)')
    31 print('指定时间 :', dt)
    32  
    33 # 3) datetime转换为timestamp
    34 dt_stamp = dt.timestamp()
    35 print('3)')
    36 print('指定时间对应时间戳 :', dt_stamp)
    37  
    38 # 4) timestamp转换为datetime
    39 t = 163423625
    40 print('4)')
    41 print('时间戳 :', t)
    42 print('对应本地时间 :', datetime.fromtimestamp(t))
    43 print('UTC标准时间 :', datetime.utcfromtimestamp(t))
    44 print('weekOfDay :', datetime.fromtimestamp(t).weekday())
    45  
    46 # 5) str转换为datetime
    47 day = datetime.strptime('2016-12-2 15:45:35', '%Y-%m-%d %H:%M:%S')
    48 print('5)')
    49 print(day)
    50  
    51 # 6) datetime转换为str
    52 now = datetime.now()
    53 print('6)')
    54 print('当前时间 :', now)
    55 print(now.strftime('%A, %B %d %H:%M, %Y'))
    56  
    57 # 7) datetime加减
    58 # datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)
    59 now = datetime.now()
    60 print('7)')
    61 print('当前时间 :', now)
    62 now_stamp = now.timestamp()
    63 print('时间戳 :', now_stamp)
    64 np.savetxt('now_stamp.txt', np.array([now_stamp]))
    65 t = now + timedelta(days=1, hours=8, minutes=5, seconds=20)
    66 print('1天8小时5分20秒之后的时间 :', t)
  • 相关阅读:
    百度搜索时加上 (要屏蔽的内容),你会回来感谢我的。
    layui中不全的组件
    PHP使用gd库做的图像处理
    范用户体验相关BLOG
    视频聊天室软件的技术标准
    QTP11使用DOM XPath以及CSS识别元素对象
    跟我一起学Oracle 11g【11】Oracle 中的事务
    一点关于 mybatis 的记录
    git撤销本地commit,复原本地代码与服务器同步
    中国人民银行令〔2021〕第4号(征信业务管理办法)
  • 原文地址:https://www.cnblogs.com/iwangzhengchao/p/9845325.html
Copyright © 2011-2022 走看看