20

時間を12時間から24時間に変換しようとしています...

自動 時間の例:

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

コード例:

m2 = "1:35" ## This is in the afternoon.
m2 = datetime.strptime(m2, "%H:%M")
print m2

期待される出力:

13:35

実際の出力:

1900-01-01 01:35:00

2 番目のバリエーションを試しましたが、やはり役に立ちませんでした :/

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")

私は何を間違っていますか?どうすれば修正できますか?

4

14 に答える 14

36

このアプローチでは、 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
于 2016-02-21T19:51:51.407 に答える
27

AM ではなく PM を意味することを指定する必要があります。

>>> from datetime import *
>>> m2 = '1:35 PM'
>>> m2 = datetime.strptime(m2, '%I:%M %p')
>>> print(m2)
1900-01-01 13:35:00
于 2013-10-07T15:57:08.103 に答える
5

これを試して :)

コード:

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

出力:

13:35
于 2013-10-07T17:59:35.747 に答える
4
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)
于 2016-04-28T17:56:30.317 に答える
1

これをきれいに行うもう1つの方法

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")

于 2018-12-13T10:20:59.917 に答える
0

時間表示に「H」を使用する代わりに、以下の例で説明されているように「I」を使用します。

from datetime import *
m2 = 'Dec 14 2018 1:07PM'
m2 = datetime.strptime(m2, '%b %d %Y %I:%M%p')
print(m2)

このリンクを確認してください

于 2020-04-14T07:21:53.547 に答える
0

手計算のほとんどは少し複雑に思えるので、私のやり方を共有します。

# Assumption: Time format (hh:mm am/pm) - You can add seconds as well if you need
def to_24hr(time_in_12hr):
    hr_min, am_pm = time_in_12hr.lower().split()
    hrs, mins = [int(i) for i in hr_min.split(":")]
    hrs %= 12
    hrs += 12 if am_pm == 'pm' else 0
    return f"{hrs}:{mins}"

print(to_24hr('12:00 AM'))
于 2021-12-23T05:56:08.623 に答える
-1

%H ゼロが埋め込まれた 10 進数としての時間 (24 時間制)。
%I ゼロが埋め込まれた 10 進数としての時間 (12 時間制)。

m2 = "1:35" ## This is in the afternoon.
m2 = datetime.strptime(m2, "<b>%I</b>:%M")
print(m2)
于 2018-04-06T17:18:51.567 に答える