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
  • 相关阅读:
    401. Binary Watch
    46. Permutations
    61. Rotate List
    142. Linked List Cycle II
    86. Partition List
    234. Palindrome Linked List
    19. Remove Nth Node From End of List
    141. Linked List Cycle
    524. Longest Word in Dictionary through Deleting
    android ListView详解
  • 原文地址:https://www.cnblogs.com/yaner2018/p/11250122.html
Copyright © 2011-2022 走看看