格式化时间
作者:互联网
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")
标签:11,-%,格式化,timeStruct,时间,time,2019,strTime 来源: https://www.cnblogs.com/zjjing/p/16575266.html