Time Conversion

Sort by

recency

|

283 Discussions

|

  • + 0 comments

    **Code in Python ** def timeConversion(s): h=s[:2] se=s[-2:] rest=s[2:-2] ho=int(h) if se=='AM': if ho==12: ho=0 else: if ho != 12: ho += 12 return f"{ho:02}{rest}"

  • + 0 comments

    define TWELVE_ASCII 99 // "1" + "2" = 49+50 = 99

    define ZERO_ASCII 48

    void ConvertToMilitarTime(string am_pm, string& hours) { uint8_t digit_sum = hours[0] + hours[1]; if(am_pm == "AM") { if(digit_sum < TWELVE_ASCII) { hours[0] = hours[0] + 1; hours[1] = hours[1] + 2; } else if(digit_sum == TWELVE_ASCII) { hours[0] = ZERO_ASCII; hours[1] = ZERO_ASCII; } } else // is PM { if(digit_sum != TWELVE_ASCII) { hours[0] = hours[0] + 1; hours[1] = hours[1] + 2; } } } /* * Complete the 'timeConversion' function below. * * The function is expected to return a STRING. * The function accepts STRING s as parameter. */

    string timeConversion(string s) { string am_pm(s, 8); string min_sec(s, 2, 6); string hours_digits(s, 0, 2);

    ConvertToMilitarTime(am_pm, hours_digits);
    string formated_time = hours_digits + min_sec;
    
    return formated_time;
    

    }

  • + 0 comments

    C#

    public static string timeConversion(string s) { string ampm = s.Substring(s.Length - 2); string[] parts = s.Substring(0, s.Length - 2).Split(":"); int hour = int.Parse(parts[0]); string minutes = parts[1]; string seconds = parts[2];

        if (ampm == "AM"){
            if (hour <12){
                s = $"{hour}:{minutes}:{seconds}";
            }
            if (hour == 12){
                hour -= 12;
            }
        }else if (ampm == "PM"){
            if (hour < 12){
                hour += 12;
                // s = $"{hour}:{minutes}:{seconds}";
            }
        }
        s = $"{hour:D2}:{minutes}:{seconds}";
        return s;
    }
    
  • + 0 comments
    def tim```eConversion(s):
        # Write your code here
        from datetime import datetime
        s_time = datetime.strptime(s, '%I:%M:%S%p')
        new_format = datetime.strftime(s_time, '%H:%M:%S')
        return new_format
    
  • + 0 comments
    function timeConversion(s) {
        let ampm = s[8];
        let ne = "";
            if(ampm == "A"){
                if (s.substring(0,2) == "12") {
                    ne = "00";
                }
                else{
                    ne = s.substring(0,2);
                }
            }
            else{
                if (s.substring(0,2) == "12") {
                    ne = s.substring(0,2);
                }
                else{
                    ne = parseInt(s.substring(0,2)) + 12;
                }
            }
        return (ne + s.substring(2,8))
    }