龙空技术网

Python计算文件创建的天数之os、time、datetime

温水青蛙202306 195

前言:

现在看官们对“c语言date类型”大概比较关切,姐妹们都想要学习一些“c语言date类型”的相关内容。那么小编在网上网罗了一些对于“c语言date类型””的相关资讯,希望朋友们能喜欢,姐妹们快快来学习一下吧!

20221203记录

import os,time,datetimefilepath = r"E:\chromedriver.exe"# 1,文件的创建时间(时间戳):# <class 'float'> 1664002851.0419765ctime = os.path.getctime(filepath)print(type(ctime),ctime)# 2,时间戳转换成本地时间:# <class 'time.struct_time'> time.struct_time(tm_year=2022, tm_mon=9, tm_mday=24, tm_hour=15, tm_min=0, tm_sec=51, tm_wday=5, tm_yday=267, tm_isdst=0)locatime = time.localtime(ctime)print(type(locatime),locatime)# 3,本地时间转换成字符串:# <class 'str'> 2022-09-24 15:00:51strtime = time.strftime("%Y-%m-%d %H:%M:%S",locatime)print(type(strtime),strtime)"""注意:datetime.datetime.strftime(),也可以进行时间转字符串,但是这里 locatime 不符合datetime的转换类型条件,会报错如下:TypeError: descriptor 'strftime' for 'datetime.date' objects doesn't apply to a 'time.struct_time' object所以如下的 strtime02 不可以,strtime03可以;strtime02 = datetime.datetime.strftime(locatime,"%Y-%m-%d %H:%M:%S")strtime03 = datetime.datetime.strftime(datetime.datetime.today(),"%Y-%m-%d %H:%M:%S")"""# 4,字符串转换成日期:# <class 'datetime.datetime'> 2022-09-24 15:00:51datime = datetime.datetime.strptime(strtime,"%Y-%m-%d %H:%M:%S")print(type(datime),datime)"""注意:time.strptime(strtime,"%Y-%m-%d %H:%M:%S") 也可以进行字符串转换成日期,但是得到的数据如下,这种日期数据不直观:<class 'time.struct_time'> time.struct_time(tm_year=2022, tm_mon=9, tm_mday=24, tm_hour=15, tm_min=0, tm_sec=51, tm_wday=5, tm_yday=267, tm_isdst=-1)所以这里使用 datetime.datetime.strptime(strtime,"%Y-%m-%d %H:%M:%S") 进行转换"""# 5,计算文件创建的天数:nowday = datetime.datetime.today()print(type(nowday),nowday) # <class 'datetime.datetime'> 2022-12-03 15:04:39.670033ndays = (nowday-datime).daysprint(ndays) # 70

标签: #c语言date类型