格式化时间

import time

#格式化时间(2019-11-27T16:29:54)
def TimeFormatS(tS):
    # t = "2019-11-27T16:29:54"
    #先转换为时间数组,然后转换为其他格式
    timeStruct = time.strptime(tS, "%Y-%m-%dT%H:%M:%S")
    strTime = time.strftime("%Y%m%d", timeStruct)
    # print(strTime)
    return strTime

#格式化时间(2019-11-27)
def TimeFormatD(tD):
    # t = "2019-11-27T16:29:54"
    #先转换为时间数组,然后转换为其他格式
    timeStruct = time.strptime(tD, "%Y-%m-%d")
    strTime = time.strftime("%Y%m%d", timeStruct)
    # print(strTime)
    return strTime

#格式化时间(2019-11->20191101)
def TimeFormatM(tM):
    timeStruct = time.strptime(tM, "%Y-%m")
    strTime = time.strftime("%Y%m%d", timeStruct)
    print(strTime)
    return strTime


#格式化时间(2019)
# def TimeFormatY(tY):
#     timeStruct = time.strptime(tY, "%Y")
#     strTime = time.strftime("%Y%m%d", timeStruct)
#     # print(strTime)
#     return strTime


if __name__ == "__main__":
    TimeFormatS("2019-11-27T16:29:54")
    TimeFormatD("2019-11-27")
    TimeFormatM("2019-11")

  

posted @ 2022-08-11 10:54  张小七soso  阅读(10)  评论(0编辑  收藏  举报