python - 从 Python 中的 getctime 、 getmtime 中提取每年、月、日、年

标签 python datetime

我想从下面的值中提取年月日小时分钟。

import os, time, os.path, datetime

date_of_created = time.ctime(os.path.getctime(folderName))
date_of_modi = time.ctime(os.path.getmtime(folderName))

现在我只能像下面这样 '2019 年 12 月 26 日星期四 19:21:37' 但我想单独获取值 2019//12 月(我可以将其作为 int 获取吗?)//26 每个

我想从 date_of_created 和 date_of_modi 中提取每年月日每次的最小值 我能拿到吗?在Python中?

最佳答案

您可以将字符串转换为日期时间对象:

from datetime import datetime
date_of_created = datetime.strptime(time.ctime(os.path.getctime(folderName)), "%a %b %d %H:%M:%S %Y") # Convert string to date format
print("Date created year: {} , month: {} , day: {}".format(str(date_of_created.year),str(date_of_created.month),str(date_of_created.day)))

关于python - 从 Python 中的 getctime 、 getmtime 中提取每年、月、日、年,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60145251/

相关文章:

python - 规范化餐厅菜肴列表

python - pyodbc - 导入错误 : DLL load failed:

python - 向我解释 .join() 在 Python 中如何工作

mysql - 使用 Perl 在日期时间格式之间转换

sql - 在oracle中获取日期

python - 为什么结果变量会自行更新?

python - python脚本中使用awk时的格式问题

python - 从 tf.data 中仅提取 numpy 数组的一部分

python - Pandas:将日期时间对象分配给时间间隔

c# - 组合时间范围