[英]Convert timespec to YYYYMMDD in Python
I am using the .st_birthtime method to get the date of creation of a file. 我正在使用.st_birthtime方法获取文件的创建日期。
The result looks like: 结果看起来像:
1359492652
1359492652
which I can convert to a more readable format 我可以将其转换为更易读的格式
2013-01-29 21:50:52
2013-01-29 21:50:52
using 运用
datetime.datetime.fromtimestamp(statinfo.st_birthtime)
My question is: how can I convert it to YYYYMMDD format? 我的问题是:如何将其转换为YYYYMMDD格式? I don't give importance of the hours and minutes.
我不重视小时和分钟。 In this example the result should be
在此示例中,结果应为
20130129
20130129
Something like the SELECT CONVERT(VARCHAR(10), @date, 112)
of T-SQL. 类似于T-SQL的
SELECT CONVERT(VARCHAR(10), @date, 112)
。 I am using Python version 3.5.3 and MacOS. 我正在使用Python版本3.5.3和MacOS。
It's this what you wanted? 这就是你想要的吗?
#time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(1359492652))
time.strftime('%Y%m%d', time.gmtime(1359492652))
I've assumed that 1359492652 is the total of seconds so this is the right date formatters for Python, tested it in Python 3 interpreter. 我假设1359492652是秒总数,因此这是Python正确的日期格式,并在Python 3解释器中对其进行了测试。 The first line which is a comment is the same result as you had with the datetime method.
第一行是注释,与使用datetime方法的结果相同。
if you want here is a link for the strftime behaviour: https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior 如果需要,这里是strftime行为的链接: https : //docs.python.org/3/library/datetime.html#strftime-strptime-behavior
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.