[英]python get time stamp on file in mm/dd/yyyy format
I'm trying to get the datestamp on the file in mm/dd/yyyy format 我正在尝试以mm / dd / yyyy格式获取文件上的日期戳
time.ctime(os.path.getmtime(file))
gives me detailed time stamp Fri Jun 07 16:54:31 2013
给我详细的时间戳Fri Jun 07 16:54:31 2013
How can I display the output as 06/07/2013
如何将输出显示为06/07/2013
You want to use time.strftime()
to format the timestamp; 您希望使用time.strftime()
来格式化时间戳; convert it to a time tuple first using either time.gmtime()
or time.localtime()
: 首先使用time.gmtime()
或time.localtime()
将其转换为时间元组:
time.strftime('%m/%d/%Y', time.gmtime(os.path.getmtime(file)))
from datetime import datetime
from os.path import getmtime
datetime.fromtimestamp(getmtime(file)).strftime('%m/%d/%Y')
You can create the datetime object using the ctime str like you mention and then format it back to a string of any format. 你可以像你提到的那样使用ctime str创建datetime对象,然后将其格式化为任何格式的字符串。
str1 = time.ctime(os.path.getmtime(file)) # Fri Jun 07 16:54:31 2013
datetime_object = datetime.strptime(str1, '%a %b %d %H:%M:%S %Y')
datetime_object.strftime("%m/%d/%Y") # 06/07/2013
This way you don't have to deal with timezones + absolute timestamp from the Epoch 这样您就不必处理来自Epoch的时区+绝对时间戳
Credit: Converting string into datetime Credit: 将字符串转换为datetime
Linking: How to get file creation & modification date/times in Python? 链接: 如何在Python中创建文件和修改日期/时间?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.