• 内置函数time


    time

     

    import time.time()   # 浮点型,给计算机看,随机

    时间有三种:

      First: 时间戳  (time.time())

      Second: 结构化时间  可以修改

      Third: 字符串时间,给用户看,用户使用

    print(time.localtime())  #结构化时间
    print(time.strftime('%Y-%m-%d %X')) 
    print(time.strftime('%Y-%m-%d %H:%M:%S'))
    #字符串时间

    将时间戳转换成字符串

    print(time.localtime(time.time()))
    f = time.localtime(time.time() - 86000 * 3)  # 150000000 秒  
    print(time.strftime('%Y-%m-%d %H:%M:%S',f))
    print(time.strftime('%Y-%m-%d %X',f))
    
    # 使用strftime方法时间戳变成字符串

    将字符串的时间转换成结构化的时间

     f = time.strptime('2018-11-30 12:30','%Y-%m-%d %H:%M')
    
    # 这个是将字符串转成结构化

    将结构化的时间转换成时间戳

    new_time = time.mktime(f) + 2*3600   # 推后两个小时
    #转换成时间戳就可以进行运算,以秒为单位

    时间的转换

    # #时间戳 -- 结构化时间
    # #结构化时间 -- 字符串时间

    # #字符串时间 -- 结构化时间
    # #结构化时间 -- 时间戳

    # 总结:
    # 6个
    # 时间戳 -- 结构化 1 ****
    # 结构化 -- 时间戳 1 ****
    # 结构化 -- 字符串 1 ****
    # 字符串 -- 结构化 1 ****
    # # time.time() 1 # 获取当前时间戳
    # # strftime() 1 # 获取当前字符串时间

    # strftime('格式','结构化时间') # 格式可以少写
    # strptime('字符串时间','格式') # 格式一一对应

  • 相关阅读:
    Eclipse在线安装主题(color theme)以及安装color theme第三方主题(图文)
    eclipse快速打开文件目录
    获取项目路径
    阿里巴巴Druid数据源及使用
    project_online
    色区-论坛
    mysql密码
    jre变更目录或修改文件名后报错解决
    9个基于Java的搜索引擎框架
    面试参考
  • 原文地址:https://www.cnblogs.com/mk-lovefreedom/p/10566693.html
Copyright © 2020-2023  润新知