Python 将 12 小时转换为 24 小时时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19229190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert 12 hour into 24 hour times
提问by Ryflex
I'm trying to convert times from 12 hour times into 24 hour times...
我正在尝试将时间从 12 小时制转换为 24 小时制...
Automatic Example times:
自动示例时间:
06:35 ## Morning
11:35 ## Morning (If m2 is anywhere between 10:00 and 12:00 (morning to mid-day) during the times of 10:00 and 13:00 (1pm) then the m2 time is a morning time)
1:35 ## Afternoon
11:35 ## Afternoon
Example code:
示例代码:
m2 = "1:35" ## This is in the afternoon.
m2 = datetime.strptime(m2, "%H:%M")
print m2
Expected Output:
预期输出:
13:35
Actual Output:
实际输出:
1900-01-01 01:35:00
I tried a second variation but again didn't help :/
我尝试了第二个变体,但再次没有帮助:/
m2 = "1:35" ## This is in the afternoon.
m2split = m2.split(":")
if len(m2split[0]) == 1:
m2 = ("""%s%s%s%s""" % ("0", m2split[0], ":", m2split[1]))
print m2
m2temp = datetime.strptime(m2, "%I:%M")
m2 = m2temp.strftime("%H:%M")
What am I doing wrong and how can I fix this?
我做错了什么,我该如何解决?
采纳答案by SMNALLY
Try this :)
尝试这个 :)
Code:
代码:
currenttime = datetime.datetime.now().time().strftime("%H:%M")
if currenttime >= "10:00" and currenttime <= "13:00":
if m2 >= "10:00" and m2 >= "12:00":
m2 = ("""%s%s""" % (m2, " AM"))
else:
m2 = ("""%s%s""" % (m2, " PM"))
else:
m2 = ("""%s%s""" % (m2, " PM"))
m2 = datetime.datetime.strptime(m2, '%I:%M %p')
m2 = m2.strftime("%H:%M %p")
m2 = m2[:-3]
print m2
Output:
输出:
13:35
回答by dan04
You need to specify that you mean PM instead of AM.
您需要指定您的意思是 PM 而不是 AM。
>>> from datetime import *
>>> m2 = '1:35 PM'
>>> m2 = datetime.strptime(m2, '%I:%M %p')
>>> print(m2)
1900-01-01 13:35:00
回答by modpy
This approach uses strptime and strftime with format directives as per https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior, %H is the 24 hour clock, %I is the 12 hour clock and when using the 12 hour clock, %p qualifies if it is AM or PM.
这种方法根据https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior使用 strptime 和 strftime 和格式指令,%H 是 24 小时制,%I 是 12 小时制当使用 12 小时制时,%p 限定是 AM 还是 PM。
>>> from datetime import datetime
>>> m2 = '1:35 PM'
>>> in_time = datetime.strptime(m2, "%I:%M %p")
>>> out_time = datetime.strftime(in_time, "%H:%M")
>>> print(out_time)
13:35
回答by NagMani
time = raw_input().strip() # input format in hh:mm:ssAM/PM
t_splt = time.split(':')
if t_splt[2][2:] == 'PM' and t_splt[0] != '12':
t_splt[0] = str(12+ int(t_splt[0]))
elif int(t_splt[0])==12 and t_splt[2][2:] == 'AM':
t_splt[0] = '00'
t_splt[2] = t_splt[2][:2]
print ':'.join(t_splt)
回答by Alessandro
Try this
尝试这个
dicti =
{'01':13,'02':14,'03':15,'04':16,'05':17,'06':18,'07':19,'08':20,'09':21,'10':22,'11':23,'12':12}
s = '12:40:22PM'
if s.endswith('AM'):
if s.startswith('12'):
s1=s[:8]
bb=s1.replace('12','00')
print bb
else:
s1=s[:8]
print s1
else:
s1=s[:8]
time= str(s[:2])
ora=str(dicti[time])
aa=s1.replace(time,ora)
print aa
回答by Vikas P
If date is in this format (HH:MM:SSPM/AM) the following code is effective :
如果日期采用这种格式 (HH:MM:SSPM/AM),则以下代码有效:
a=''
def timeConversion(s):
if s[-2:] == "AM" :
if s[:2] == '12':
a = str('00' + s[2:8])
else:
a = s[:-2]
else:
if s[:2] == '12':
a = s[:-2]
else:
a = str(int(s[:2]) + 12) + s[2:8]
return a
s = '11:05:45AM'
result = timeConversion(s)
print(result)
回答by szerem
%H
Hour (24-hour clock) as a zero-padded decimal number.%I
Hour (12-hour clock) as a zero-padded decimal number.
%H
小时(24 小时制)作为补零的十进制数。%I
小时(12 小时制)作为用零填充的十进制数。
m2 = "1:35" ## This is in the afternoon.
m2 = datetime.strptime(m2, "<b>%I</b>:%M")
print(m2)
回答by Alok Kumar Singh
One more way to do this cleanly
另一种干净利落的方法
def format_to_24hr(twelve_hour_time):
return datetime.strftime(
datetime.strptime(
twelve_hour_time, '%Y-%m-%d %I:%M:%S %p'
), "%Y-%m-%d %H:%M:%S")
def format_to_24hr(twelve_hour_time):
return datetime.strftime(
datetime.strptime(
twelve_hour_time, '%Y-%m-%d %I:%M:%S %p'
), "%Y-%m-%d %H:%M:%S")