Time Conversion

Sort by

recency

|

362 Discussions

|

  • + 0 comments

    for python3:

    is_pm = s.endswith('PM') time_part = s[:-2] hours = int(time_part[:2])

    if is_pm:
        if hours != 12:
            hours += 12
    else: 
        if hours == 12:
            hours = 0   
    
    military_hours = f"{hours:02d}"
    result = military_hours + time_part[2:]
    return result
    
  • + 0 comments
    def timeConversion(s):
        # Write your code here
        hour = s[0:2]
        remaining = s[2:8]
        time_format = s[8:10]
        if time_format == "PM":
            if int(hour) < 12:
                hour=int(hour)+12
            elif int(hour) > 12:
                hour=int(hour)+1        
        elif time_format == "AM":
    
        if int(hour) == 12:
            hour="00"
    return(str(hour)+remaining)
    
  • + 0 comments

    i did not use the datetime function as I figured it is a shortcut this is my appraoch:

    def timeConversion(s): # Write your code here l = s.split(":")

    result = ""
    am_pm = l[-1][2:]
    sec = l[-1][0:2]
    l.pop()
    l.append(sec)
    for e in l:
        print(e)
        if am_pm == "AM":
            if e == "12":
                l[0] = "00"
    
            else:
                result + e
        else:
            if e == "12":
                break
            else:
                l[0] = result + str(int(l[0]) + 12)
                break
    
    
    return(":".join(l))
    
  • + 0 comments
        public static string timeConversion(string s)
        {
            DateTime d = DateTime.Parse(s);
            
            return $"{d:HH:mm:ss}";
        }
    
  • + 0 comments

    my approach:

    function timeConversion($s) {
        $mili = date('H:i:s', strtotime($s));
        return $mili;
    }