我有一个函数,可以在几秒钟内返回信息,但我需要以小时:分钟:秒存储该信息.
有没有一种简单的方法可以在Python中将秒转换为这种格式?
Please, how to convert an int (number a seconds) to these formats: mm:ss or hh:mm:ss ?
I need to do this with Python code (and if possible in a Django template ?).
Thank you very much ;-)
我对python很新.我有两个日期时间对象.我需要计算它们之间的时间值,然后以特定格式显示输出.
Alpha_TimeObj = datetime.datetime(int(AlphaTime.strftime('%Y')), int(AlphaTime.strftime('%m')), int(AlphaTime.strftime('%d')), int(AlphaTime.strftime('%H')), int(AlphaTime.strftime('%M')), int(AlphaTime.strftime('%S')))
Beta_TimeObj = datetime.datetime(int(BetaTime.strftime('%Y')), int(BetaTime.strftime('%m')), int(BetaTime.strftime('%d')), int(BetaTime.strftime('%H')), int(BetaTime.strftime('%M')), int(BetaTime.strftime('%S')))
Turnaround_TimeObj = Beta_TimeObj - Alpha_TimeObj
Run Code Online (Sandbox Code Playgroud)
此Turnaround_TimeObj时间增量的一个示例是"2天,22:13:45".我想格式化输出,但我无法这样做.
print Turnaround_TimeObj.strftime('%H hrs %M mins %S secs')
Run Code Online (Sandbox Code Playgroud)
不起作用.
我知道这样做的一种方法是将其转换为秒,然后进行divmodding以获得所需的格式.如;
totalSeconds = Turnaround_TimeObj.seconds
hours, remainder = divmod(totalSeconds, 3600)
minutes, seconds = divmod(remainder, 60)
print '%s:%s:%s' % (hours, minutes, seconds)
Run Code Online (Sandbox Code Playgroud)
但我想知道我是否可以使用像strftime这样的日期时间函数在一行中完成它.
编辑:实际上转换为秒也不起作用.如果我将时间增量"1天,3:42:54"转换为秒使用
totalSeconds = Turnaround_TimeObj.seconds
Run Code Online (Sandbox Code Playgroud)
totalSeconds值显示为13374而不是99774. ie.它忽略了"日"的价值.
我给出了这样的日期字符串:
Mon Jun 28 10:51:07 2010
Fri Jun 18 10:18:43 2010
Wed Dec 15 09:18:43 2010
Run Code Online (Sandbox Code Playgroud)
什么是方便的python方式来计算天数的差异?假设时区是相同的.
这些字符串由linux命令返回.
编辑:谢谢,这么多好的答案
我有一个数据框df,它的第一列是timedelta64
df.info():
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 686 entries, 0 to 685
Data columns (total 6 columns):
0 686 non-null timedelta64[ns]
1 686 non-null object
2 686 non-null object
3 686 non-null object
4 686 non-null object
5 686 non-null object
Run Code Online (Sandbox Code Playgroud)
print(df[0][2])例如,如果 I ,它会给我0 days 05:01:11。但是,我不想要0 days归档。我只想05:01:11被打印。有人可以教我如何做到这一点吗?非常感谢!
我想以时间格式HH:MM:SS转换十进制时间列表(HH,HHH)
16. , 16.00000381, 16.00000572, 16.00000954,
16.00001144, 16.00001335, 16.00001717, 16.00001907,
16.00002098, 16.0000248 , 16.00002861, 16.00003052,
16.00003433, 16.00003624, 16.00003815, 16.00004196,
16.00004387, 16.00004768, 16.00004959, 16.00005341
Run Code Online (Sandbox Code Playgroud)
有没有办法在python中执行此操作?
谢谢
我是python的新手。我试图显示持续时间。我所做的是:
startTime = datetime.datetime.now().replace(microsecond=0)
... <some more codes> ...
endTime = datetime.datetime.now().replace(microsecond=0)
durationTime = endTime - startTime
print("The duration is " + str(durationTime))
Run Code Online (Sandbox Code Playgroud)
输出是 => 持续时间是 0:01:28 我可以知道如何从结果中删除小时吗?我要显示 => 持续时间是 01:28
提前致谢!
python ×7
datetime ×4
formatting ×2
timedelta ×2
decimal ×1
django ×1
duration ×1
pandas ×1
time ×1
time-format ×1