zoukankan      html  css  js  c++  java
  • python_base_时间戳和日期相互转换

    #在python里时间戳可以通过time模块的time()方法获取,如下:
    import time

    print(time.time())

    输出结果:1544684606.9632869


    #时间戳→指定格式的日期格式:
    import time

    st=time.localtime(1544758200)
    sr=time.strftime('%Y-%m-%d %H:%M:%S', st) #strftime() 函数接收以时间元组,并返回以可读字符串表示的当地时间,格式由参数format决定
    print(sr)

    输出结果:2018-12-14 11:30:00


    #时间格式→时间戳:
    import time

    st=time.strptime("2018-12-18 11:30:00","%Y-%m-%d %H:%M:%S") #strptime() 函数根据指定的格式把一个时间字符串解析为时间元组
    sr=time.mktime(st) #接收struct_time对象作为参数,返回用秒数来表示时间的浮点数

    print(sr)

    输出结果:1545103800.0


    #获取当前时间:
    print(time.strftime("%Y-%m-%d  %H:%M:%S"))    %m 小写m 表示月份(01-12)
    输出结果:2018-12-13  15:37:06
    使用python语言,编写一个函数,将时间戳(格式如:1416206140)的对应时间增加一天后再格式化(xxxx-xx-xx  xx:xx:xx)输出。
    import datatime

    def timeChange(timeStamp):
    st=datetime.datetime.fromtimestamp(timeStamp)+ datetime.timedelta(days = 1)

    print(st.strftime('%Y-%m-%d %H:%M:%S'))


    timeChange(1416206140)


    不用函数,实现时间戳的转换:
    st=time.localtime(1416206140)
    sr=time.strftime('%Y-%m-%d %H:%M:%S', st)

    print(sr)
    
    
     


  • 相关阅读:
    python内置函数
    conda和anaconda的区别
    闭包,装饰器,property
    【模板】大数乘法(51nod 1027)
    51nod 1791 合法括号子段
    51nod 1419 最小公倍数挑战
    51nod 1241 特殊的排序
    51nod 1090 3个数和为0
    【模板】51nod 1051 最大子矩阵和
    51nod 1267 4个数和为0
  • 原文地址:https://www.cnblogs.com/tianpin/p/10114485.html
Copyright © 2011-2022 走看看