📌  相关文章
📜  将时间从24小时制转换为12小时制

📅  最后修改于: 2021-05-31 21:48:39             🧑  作者: Mango

给定时间为24小时制(军事时间),请将其转换为12小时制。
注意:午夜在24小时制是00:00:00,在12小时制是12:00:00 AM。中午在24小时制是12:00:00,在12小时制是12:00:00 PM。
将以hh:mm:ss的格式给出一个字符串,并且输出应以12小时制的hh:mm:ss AM或hh:mm:ss PM格式。这里hh代表小时,mm代表分钟,ss代表秒。
例子

Input : 17:35:20
Output : 5:35:20 PM

Input : 00:10:24
Output : 12:10:24 AM

解决此问题的方法需要一些观察。首先,两种情况下的分钟和秒都相同。唯一的更改是在小时中,并根据该时间将Meridien附加到字符串。
对于小时,首先将其从字符串转换为int数据类型,然后将其取模为12,这将是我们的12小时格式的小时。仍然有一种情况,当小时变为00时(即24小时制为12或00),我们需要分别处理。
下面是上述方法的实现:

C++
// CPP program to convert time from 24 hour
// to 12 hour format
 
#include 
 
using namespace std;
 
// Convert Function which takes in
// 24hour time and convert it to
// 12 hour format
void convert12(string str)
{
    // Get Hours
    int h1 = (int)str[0] - '0';
    int h2 = (int)str[1] - '0';
 
    int hh = h1 * 10 + h2;
 
    // Finding out the Meridien of time
    // ie. AM or PM
    string Meridien;
    if (hh < 12) {
        Meridien = "AM";
    }
    else
        Meridien = "PM";
 
    hh %= 12;
 
    // Handle 00 and 12 case separately
    if (hh == 0) {
        cout << "12";
 
        // Printing minutes and seconds
        for (int i = 2; i < 8; ++i) {
            cout << str[i];
        }
    }
    else {
        cout << hh;
        // Printing minutes and seconds
        for (int i = 2; i < 8; ++i) {
            cout << str[i];
        }
    }
 
    // After time is printed
    // cout Meridien
    cout << " " << Meridien << '\n';
}
 
// Driver code
int main()
{
    // 24 hour format
    string str = "17:35:20";
 
    convert12(str);
 
    return 0;
}


Java
// Java program to convert time from 24 hour
// to 12 hour format
 
import java.util.*;
// Convert Function which takes in
// 24hour time and convert it to
// 12 hour format
class GFG
{
 
static void convert12(String str)
{
// Get Hours
    int h1 = (int)str.charAt(0) - '0';
    int h2 = (int)str.charAt(1)- '0';
 
    int hh = h1 * 10 + h2;
 
    // Finding out the Meridien of time
    // ie. AM or PM
    String Meridien;
    if (hh < 12) {
        Meridien = "AM";
    }
    else
        Meridien = "PM";
 
    hh %= 12;
 
    // Handle 00 and 12 case separately
    if (hh == 0) {
        System.out.print("12");
 
        // Printing minutes and seconds
        for (int i = 2; i < 8; ++i) {
        System.out.print(str.charAt(i));
        }
    }
    else {
        System.out.print(hh);
        // Printing minutes and seconds
        for (int i = 2; i < 8; ++i) {
        System.out.print(str.charAt(i));
        }
    }
 
    // After time is printed
    // cout Meridien
System.out.println(" "+Meridien);
}
 
//Driver code
public static void main(String ar[])
{
 
// 24 hour format
    String str = "17:35:20";
    convert12(str);
 
}
}


Python3
# Python program to convert time from 24 hour
# to 12 hour format
 
# Convert Function which takes in
# 24hour time and convert it to
# 12 hour format
def convert12(str):
 
    # Get Hours
    h1 = ord(str[0]) - ord('0');
    h2 = ord(str[1]) - ord('0');
 
    hh = h1 * 10 + h2;
 
    # Finding out the Meridien of time
    # ie. AM or PM
    Meridien="";
    if (hh < 12):
        Meridien = "AM";
    else:
        Meridien = "PM";
 
    hh %= 12;
 
    # Handle 00 and 12 case separately
    if (hh == 0):
        print("12", end = "");
 
        # Printing minutes and seconds
        for i in range(2, 8):
            print(str[i], end = "");
 
    else:
        print(hh,end="");
         
        # Printing minutes and seconds
        for i in range(2, 8):
            print(str[i], end = "");
 
    # After time is printed
    # cout Meridien
    print(" " + Meridien);
 
# Driver code
if __name__ == '__main__':
 
    # 24 hour format
    str = "17:35:20";
    convert12(str);
 
# This code is contributed by 29AjayKumar


C#
// C# program to convert time from 24 hour
// to 12 hour format
  
using System;
// Convert Function which takes in
// 24hour time and convert it to
// 12 hour format
class GFG
{
  
static void convert12(string str)
{
// Get Hours
    int h1 = (int)str[0] - '0';
    int h2 = (int)str[1]- '0';
  
    int hh = h1 * 10 + h2;
  
    // Finding out the Meridien of time
    // ie. AM or PM
    string Meridien;
    if (hh < 12) {
        Meridien = "AM";
    }
    else
        Meridien = "PM";
  
    hh %= 12;
  
    // Handle 00 and 12 case separately
    if (hh == 0) {
        Console.Write("12");
  
        // Printing minutes and seconds
        for (int i = 2; i < 8; ++i) {
        Console.Write(str[i]);
        }
    }
    else {
        Console.Write(hh);
        // Printing minutes and seconds
        for (int i = 2; i < 8; ++i) {
        Console.Write(str[i]);
        }
    }
  
    // After time is printed
    // cout Meridien
Console.WriteLine(" "+Meridien);
}
  
//Driver code
public static void Main()
{
  
// 24 hour format
    string str = "17:35:20";
    convert12(str);
  
}
}


PHP


Javascript


输出:
5:35:20 PM
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”