Time Conversion

Sort by

recency

|

5052 Discussions

|

  • + 0 comments
    # two options:
    
    def timeConversion(s):
        from datetime import datetime
        in_time = datetime.strptime(s, "%I:%M:%S%p")
        out_time = datetime.strftime(in_time, "%H:%M:%S")
        return out_time
    
    def timeConversion(s):
        if s[-2:] == 'AM':
            if s[:2] == '12':
                res = '00' + s[2:-2]
            else:
                res =  s[:-2]
        else:
            if s[:2] == '12':
                res = '12' + s[2:-2]
            else:
                res = f'{int(s[:2]) + 12}{s[2: -2]}'
        return res
    
  • + 0 comments
    // here is my code in c++ guys any feedbacks 
    string timeConversion(string s) {
        string frm = s.substr(0 , 2) + s.substr((s.size()-2), 2);
        std::unordered_map<std::string, std::string> hourMap = {
            {"12AM", "00"}, {"01AM", "01"}, {"02AM", "02"}, {"03AM", "03"},
            {"04AM", "04"}, {"05AM", "05"}, {"06AM", "06"}, {"07AM", "07"},
            {"08AM", "08"}, {"09AM", "09"}, {"10AM", "10"}, {"11AM", "11"},
            {"12PM", "12"}, {"01PM", "13"}, {"02PM", "14"}, {"03PM", "15"},
            {"04PM", "16"}, {"05PM", "17"}, {"06PM", "18"}, {"07PM", "19"},
            {"08PM", "20"}, {"09PM", "21"}, {"10PM", "22"}, {"11PM", "23"}
        };
        return (hourMap[frm] + s.substr(2, 6));
    }
    
  • + 0 comments

    Here is my c++ code

    string timeConversion(string s) {
    string hour= s.substr(0,2);
    string ms= s.substr(2,6);
    string meridian= s.substr(8,2);
    int clock = stoi(hour);
    if(meridian=="AM")
    {
        if(clock==12)
        {
            hour="00";
        }
    }
    else if(meridian =="PM")
    {
        if(hour!="12")
        {
            clock+=12;
            hour  =to_string(clock);
        }
    }
    return hour+ms;
    }
    
  • + 0 comments

    string timeConversion(string s) {

    int i;
    int h1 = (int)s[1] - '0';
    int h2 = (int)s[0] - '0';
    int hh = (h2 * 10 + h1 % 10);
    string t,l;
    t=s;
    
    t[9]='\0';
    t[8]='\0';
    
    if (s[8] == 'A')
    {
        if (hh == 12)
        {
            t[0]='0';
            t[1]='0';
        }
        else
        {
    
        }
    }
    

    // If time is in "PM" else { if (hh == 12) {

        }
        else
        {
            hh = hh + 12;
            l=to_string(hh);
            t[0]=l[0];
            t[1]=l[1];
        }
    }
    
    return t;
    

    }

    Why is this wrong??

    Why is this wrong??Why is this wrong??

  • + 0 comments
    function timeConversion(s) {
        // Write your code here
      let sustitute = ""; 
      let newtime = "";
      
      // Debugging
     // s = "12:01:00PM"
    
    
       
       
       if (s.includes("AM")){
        
          if (s.substring(0,2) === "12"){
            
                  sustitute = "00"    
             }
           
            }
        
       
       
       if (s.includes("PM")){
              
        // 1 - 11  
            if(s[0]=== "0" || s[0]=== "1" && s[1] !== "2" ){
                 sustitute =  `${Number(s.substring(0,2)) + 12}`   
          
           }
        
           
        } 
     
      
      newtime = sustitute !== "" ? newtime = s.replace(`${s[0]}${s[1]}`, `${sustitute}`) : s ;
      return newtime.replace(`${s[8]}${s[9]}`, "");  
      
        
    }