zoukankan      html  css  js  c++  java
  • python 日期、时间

    
    

    1、字符类型的时间 转为 时间数组

    t1 = '2013-10-10 23:40:00'
    timeArray = time.strptime(t1, "%Y-%m-%d %H:%M:%S")

    # 结果如下
    time.struct_time(tm_year=2013, tm_mon=10, tm_mday=10, tm_hour=23, tm_min=40, tm_sec=0, tm_wday=3, tm_yday=283, tm_isdst=-1)

    2、字符类型的时间 转为其它显示格式

    t2 = "2013-10-10 23:40:00"
    # 转为数组
    timeArray = time.strptime(t2, "%Y-%m-%d %H:%M:%S")
    # 转为其它显示格式
    otherStyleTime = time.strftime("%Y/%m/%d %H:%M:%S", timeArray)
    print otherStyleTime # 2013/10/10 23:40:00

    3、获取当前时间戳

    # time获取当前时间戳
    now = int(time.time()) 
    timeArray = time.localtime(now)

    转换成需要的格式

    otherStyleTime = time.strftime("%Y--%m--%d %H:%M:%S", timeArray)

    4、datetime获取当前时间,数组格式
    now = datetime.datetime.now()
    print now
    otherStyleTime = now.strftime("%Y--%m--%d %H:%M:%S")

    1 def getlogname():
    2     logfilename = time.strftime('%Y%m%d%H%M%S',time.localtime(time.time()))
    3     return logfilename
    4 
    5 def getPresentTime():   
    6     now = datetime.datetime.now()
    7     #print(now)
    8     return now
  • 相关阅读:
    [非专业翻译] Mapster
    [非专业翻译] Mapster
    排序之猴子算法
    1309游客统计
    1631低洼地
    1636车牌问题
    1638图形
    这是一篇小短文
    1500【自定义函数】走楼梯
    PHP 之表单提交大数据,数据不完整
  • 原文地址:https://www.cnblogs.com/yaner2018/p/11250122.html
Copyright © 2011-2022 走看看