BLA*_*HER 4 python django datetime
我有,
timestamp=[]
for x in model_obj:
timestamp.append(x.start_time)
print timestamp
Run Code Online (Sandbox Code Playgroud)
结果:
[datetime.datetime(2014, 9, 2, 19, 40, 1, tzinfo=<UTC>), datetime.datetime(2014, 9, 2, 19, 40, 14, tzinfo=<UTC>), datetime.datetime(2014, 9, 2, 19, 40, 7, tzinfo=<UTC>), datetime.datetime(2014, 9, 2, 21, 53, tzinfo=<UTC>), datetime.datetime(2014, 9, 2, 22, 25, 24, tzinfo=<UTC>), datetime.datetime(2014, 9, 2, 22, 2
5, 52, tzinfo=<UTC>)]
Run Code Online (Sandbox Code Playgroud)
我的问题是:如何从python的这个日期时间列表中获取最早和最晚的时间?
使用内置min()/ max()功能:
earliest = min(timestamp)
latest = max(timestamp)
Run Code Online (Sandbox Code Playgroud)
timestamp = [x.start_time for x in model_obj]
Run Code Online (Sandbox Code Playgroud)
更新:最早的可表示datetime是0001-01-01 00:00:00因为据我所知,0000-00-00日期无法从数据库加载,你得到None的x.Date_Time_End字段.
问题是:您的任务是否允许您忽略此日期时间?如果是,则只过滤掉None列表中的值:
end = [x.Date_Time_End for model2_obj if x.Date_Time_End]
end_time = max(end)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1599 次 |
| 最近记录: |