zoukankan      html  css  js  c++  java
  • python(八)time模块

    1.time.strftime(%Y-%m-%d  %H:%M:%S)   #获取当前格式化好了的时间

    2.time.time()   #获取当前的时间戳

    3. time.gmtime()#获取的是标准时区的格式化好了的时间

        time.localtime()#获取的是当地的格式化好了的时间

    4.时间戳和格式化好的时间之间的相互转换:需要引入时间元组

    a .时间戳转换成格式化好的时间

    time_tuple=time.localtime()

    print(time.strftime('%Y-%m-%d %H:%M:%S',time_tuple))

    def timezone_to_str(timezone=None,format="%Y-%m-%d %H:%M:%S"):
        '''这个函数是时间戳转格式化好的时间,如果不传参数,默认返回当前时间'''
        if timezone:
            time_tuple = time.localtime(timezone)
            result = time.strftime(format,time_tuple)
        else:
            result = time.strftime(format)
        return result

    b. 格式化好的时间转换成时间戳

    time_tuple=time.strftime('2019-06-02','%Y-%m-%d %H:%M:%S')

    print(time.mktime(time_tuple))

    def str_to_timezone(str=None,format="%Y-%m-%d %H:%M:%S"):
        #这个函数是格式化好的时间转时间戳的,如果不传参数默认返回当前时间戳
        if str:
            time_tuple = time.strptime(str,format)
            result = time.mktime(time_tuple)
        else:
            result = time.time()
        return int(result)
  • 相关阅读:
    replace和translate的用法
    java中静态方法和静态类的学习
    rank()函数的使用
    orcle函数的使用,及其调用
    父子级菜单的查询
    Centos7 安装K8S 小记
    三剑客之三 Awk小记
    三剑客之二 Sed小记
    三剑客之一 Grep小记
    ssh与telnet区别 小记
  • 原文地址:https://www.cnblogs.com/dmjsd/p/11166044.html
Copyright © 2011-2022 走看看