Time Conversion

Sort by

recency

|

5088 Discussions

|

  • + 0 comments

    function timeConversion(s) { // Write your code here // take the 12-hour-format AM or PM let timeFormat = s.slice(-2) let hourHand = s.slice(0,2) let minuteHand = s.slice(3,5) let secondsHand = s.slice(6,8) if(timeFormat == "AM" && hourHand == '12'){ let militaryHourHand = Number(hourHand)-12 return 0{minuteHand}:${secondsHand} } else if(timeFormat == 'PM'){ if(hourHand == '01' || hourHand == '02' || hourHand == '03' || hourHand == '04' || hourHand == '05' || hourHand == '06' || hourHand == '07' || hourHand == '01' || hourHand == '08' ||hourHand == '09' || hourHand == '10' || hourHand == '11'){ let militaryHourHand = Number(hourHand) + 12 return ${militaryHourHand}:$`{minuteHand}:${secondsHand}` } else { return{minuteHand}:${secondsHand} }
    } else { return {minuteHand}:${secondsHand} }

    }

  • + 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;
    }