Python将时间为AM的转换为24小时制

Below is my code , totally C type programmer, no excellent skills.

import sys

#time = input().strip()
time = "00:05:45PM"

"""
I don't know this issue.
12:24:35AM -> 0:24:35
12:24:35PM -> 12:24:35
"""
print(time)

if time.find("AM") >= 0:
    time = time.strip("AM")
    if time == "12:00:00":
        time = "00:00:00"

    time = time.split(':')

    hh = int(time[0])
    mm = int(time[1])
    ss = int(time[2])
    if hh == 12 and (mm > 0 or ss > 0):
        hh = 0


    #print(time)
    print("%02d:%02d:%02d" % (hh, mm, ss))
else:

    time = time.strip("PM")
    time = time.split(':')

    hh = int(time[0])
    mm = int(time[1])
    ss = int(time[2])
    #print (time[0])
    #print (time[1])
    #print (time[2])

    if hh != 12:
        hh = hh + 12

    print("%02d:%02d:%02d" % (hh, mm, ss))

This is done by someone expert.
We’re in different level.

You find the Python type grammer.

string operation, list convert, map usage, and join, lambda.

ins = input().strip()

is_pm = ins[-2:].lower() == 'pm'
time_list = list(map(int, ins[:-2].split(':')))

if is_pm and time_list[0] < 12:
    time_list[0] += 12

if not is_pm and time_list[0] == 12:
    time_list[0] = 0

print(':'.join(map(lambda x: str(x).rjust(2, '0'), time_list)))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

钱国正

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值