Time Conversion

Sort by

recency

|

893 Discussions

|

  • + 0 comments

    Scala-

    ` def timeConversion(s: String): String = { // Write your code here var hour = s.substring(0, 2).toInt val amPm = s.takeRight(2)

        if (amPm == "AM") {
            if (hour == 12) hour = 0
            } else if (amPm == "PM") {
        if (hour != 12) hour += 12
        }
        f"`$hour%02d$`{s.substring(2, 8)}"
    }
    

    `

  • + 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}")