zoukankan      html  css  js  c++  java
  • python之time模块:获取当前时间

    time模块举例

     1 import time
     2 
     3 # 获取当前时间戳
     4 t = time.time()
     5 print('1)获取当前时间戳:', t)
     6 
     7 # 当前时间的struct_time形式
     8 t = time.localtime()
     9 print('2)当前时间的struct_time形式:', t)
    10 print('3)当前年份:', t.tm_year)
    11 
    12 # 当前时间的字符串形式
    13 t = time.ctime()
    14 print('4)当前时间的字符串形式:', t)
    15 
    16 # 当前时间自定义格式的字符串
    17 t = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    18 print('5)当前时间自定义格式的字符串:', t)
    19 
    20 # 将字符串转换为time对象,并改变时间格式
    21 str = '20161228080000'
    22 time_str = time.strptime(str, '%Y%m%d%H%M%S')
    23 print("6)字符串转换成time对象:", time_str)
    24 t = time.strftime('%Y-%m-%d %H:%M:%S', time_str)
    25 print("7)转换时间格式:", t)

    运行结果:

    1 1)获取当前时间戳: 1502420198.8323882
    2 2)当前时间的struct_time形式: time.struct_time(tm_year=2017, tm_mon=8, tm_mday=11, 
    3                         tm_hour=10, tm_min=56, tm_sec=38, tm_wday=4, tm_yday=223, tm_isdst=0)
    4 3)当前年份: 2017
    5 4)当前时间的字符串形式: Fri Aug 11 10:56:38 2017
    6 5)当前时间自定义格式的字符串: 2017-08-11 10:56:38
    7 6)字符串转换成time对象: time.struct_time(tm_year=2016, tm_mon=12, tm_mday=28, 
    8                             tm_hour=8, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=363, tm_isdst=-1)
    9 7)转换时间格式: 2016-12-28 08:00:00
  • 相关阅读:
    Springboot之配置定时任务
    Java之随机生成各种类型的数据举例
    docker安装postgresql 12.4
    Java随机生成字符串举例
    Java生成UUID
    Shell case 多选择语句用法举例
    Elasticsearch学习之集群高级网络设置
    交叉熵损失函数
    将博客搬至CSDN
    SonarQube服务搭建
  • 原文地址:https://www.cnblogs.com/gongxr/p/7291731.html
Copyright © 2011-2022 走看看