简体   繁体   English

如何将 python datetime.datetime 转换为列表

[英]how to convert a python datetime.datetime into a list

I have a datetime.datetime object.我有一个 datetime.datetime 对象。 All I want to do is pull it's data out and put it into a list.我想要做的就是将它的数据拉出来并放入一个列表中。 The only method I have found is:我发现的唯一方法是:

date_list=list(my_dt_ob.timetuple())

This seems to be an awfully convoluted way of doing a conceptually simple thing.这似乎是做一个概念上简单的事情的一种非常复杂的方式。 Anyone have a better way?有人有更好的方法吗? Thanks.谢谢。

Each of the parts of the date and time are available as attributes of the datetime object.每个日期和时间的零件都可用作为属性datetime对象。 You can use them directly:您可以直接使用它们:

date_list = [my_dt_ob.year, my_dt_ob.month, my_dt_ob.day, my_dt_ob.hour, my_dt_ob.minute, my_dt_ob.second]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM