Time Conversion

Sort by

recency

|

892 Discussions

|

  • + 0 comments

    DateTime input = DateTime.ParseExact(s, "hh:mm:sstt", System.Globalization.CultureInfo.InvariantCulture); return input.ToString("HH:mm:ss");

  • + 0 comments
    def timeConversion(s):
        from datetime import datetime
        return(datetime.strptime(s, '%I:%M:%S%p').strftime("%H:%M:%S"))
    

    `You may not like it but this is what peak performance looks like. :-)

  • + 0 comments
    def timeConversion(s):
        # get the time indicator
        time_indicator = s[-2:]
        
        # extract minutes and seconds because they stay fixed
        # format (e.g.): ":00:00"
        mins_secs = s[2:-2]
        
        # extract hour (the thing to alter if necessary)
        hour = s[:2]
        
        # handle edge cases (i.e. 12)
        if hour == "12":
            if time_indicator == "AM":
                military_time = "00" + mins_secs
            else:
                military_time = hour + mins_secs
        # all the other hours
        else:
            if time_indicator == "AM":
                military_time = hour + mins_secs
            else:
                # add leading zero if not already two digits
                adjusted_hour = "{:0>2}".format(str(int(hour) + 12))
                military_time = adjusted_hour + mins_secs
                
        return military_time
    
  • + 0 comments
    def timeConversion(s):
        # Write your code here
        hours = s[:2]
        minutes = s[2:5]
        seconds = s[5:8]
    
    am = s[8:]
    if am == "AM":
        if hours == "12":
            return(f"00{minutes}{seconds}")
        else:
            return(f"{hours}{minutes}{seconds}")
    else :
        if hours == "12":
            return(f"12{minutes}{seconds}")
        else:
            time = int(hours) + 12
            return(f"{time}{minutes}{seconds}")
    
  • + 0 comments

    function timeConversion(time12h) { const period = time12h.slice(-2); // "AM" or "PM" let [hour, minute, second] = time12h.slice(0, -2).split(':');

    if (period === 'PM' && hour !== '12') {
        hour = String(parseInt(hour, 10) + 12);
    } else if (period === 'AM' && hour === '12') {
        hour = '00';
    }
    
    return ``${hour.padStart(2, '0')}:$`{minute}:${second}`;
    

    }