Time Conversion

Sort by

recency

|

5087 Discussions

|

  • + 0 comments
    def timeConversion(s):
        # Write your code here
        if s[-2] == "P" and s[0:2] != "12":
            o = int(s[0:2])+ 12
            return f"{str(o)}:{s[3:8]}"
        elif  s[-2] == "A" and s[:2] == "12":
            return f"00:{s[3:8]}"
        else:
            return s[:-2]
    
  • + 0 comments

    Another C# Solution

    public static string timeConversion(string s)
        {
            var hr = s.Split(":");
            
            var hora = Convert.ToInt32(hr[0]);
            if(hr[2].Contains("PM"))
            {
                if(hora != 12)
                    hora += 12;
                
                hr[0]= hora.ToString("D2");
                    
                hr[2] = hr[2].Replace("PM", "");
            }
            else
            {
                if(hora == 12)
                {
                    hora = 0;
                }
                
                hr[0] = hora.ToString("D2");
                hr[2] = hr[2].Replace("AM", "");
            }
            
            return $"{hr[0]}:{hr[1]}:{hr[2]}";
        }
    
  • + 0 comments
    1. In Python
    2. def time_conversion(n):
    3. hour = n[0]+n[1]
    4. minute = n[3]+n[4]
    5. second = n[6]+n[7]
    6. if(n[8]=="P" and int(hour)<12):
    7. hour = int(hour)
    8. hour += 12
    9. hour = str(hour)
    10. print(f"{hour}:{minute}:{second}")
    11. elif(n[8]=="A" and int(hour)==12):
    12. print(f"00:{minute}:{second}")
    13. else:
    14. print(f"{hour}:{minute}:{second}")
    15. n = input()
    16. time_conversion(n)
  • + 0 comments

    Here is my c++ solution, you can watch the vidéo here : https://youtu.be/G7bia__Vxqg

    int main() {
        string s;
        cin >> s;
        int h;
        sscanf(s.c_str(), "%d", &h);
        if(s[8] == 'A' && h == 12) h = 0;
        if(s[8] == 'P' && h != 12) h+=12;
        cout << setw(2) << setfill('0') << h;
        cout << s.substr(2, 6);
        return 0;
    }
    
  • + 0 comments

    Typescript:

    let hours = s.split(':')[0].toString();
    let minutes = s.split(':')[1].toString();
    let seconds = (s.split(':')[2].slice(0, 2)).toString();
    let timeType = s.split(':')[2].slice(-2);
    
    if (timeType == 'AM' && hours == '12') {
       hours = '00';
    } else if (timeType == 'PM' && Number(hours) < 12) {
       hours = (Number(hours) + 12).toString();
    }
    
    return `${hours}:${minutes}:${seconds}`;