Time Conversion

Sort by

recency

|

890 Discussions

|

  • + 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}`;
    

    }

  • + 0 comments

    I touch memory,

    Rust

    use std::env;
    use std::fs::File;
    use std::io::{self, BufRead, Write};
    
    /*
     * Complete the 'timeConversion' function below.
     *
     * The function is expected to return a STRING.
     * The function accepts STRING s as parameter.
     */
    
    fn timeConversion(s: &str) -> String {
        let chars :Vec<char> = s.chars().collect();
        let am_pm = chars[8] == 'A'; 
        let mid = chars[0] == '1' && chars[1] == '2';
        return match am_pm {
            true => {
                
                if mid {
                    String::from({
                        format!(
                            "00{}",
                            chars.iter().skip(2).take(6).collect::<String>()
                        )
                    })
                } else {
                    String::from({
                        chars.iter().take(8).collect::<String>()
                    })
                }  
            },
            false => {
                if mid {
                    String::from({
                        chars.iter().take(8).collect::<String>()
                    })
                } else {
                    let first_two = chars[0].to_digit(10).unwrap() * 10 + chars[1].to_digit(10).unwrap() + 12;
                    if first_two == 24 {
                        first_two == 0;
                    }
                    String::from({
                        format!(
                            "{:0>2}{}",
                            first_two,
                            chars.iter().skip(2).take(6).collect::<String>()
                        )
                    })
                }
            }
        }
    }
    
    fn main() {
        let stdin = io::stdin();
        let mut stdin_iterator = stdin.lock().lines();
    
        let mut fptr = File::create(env::var("OUTPUT_PATH").unwrap()).unwrap();
    
        let s = stdin_iterator.next().unwrap().unwrap();
    
        let result = timeConversion(&s);
    
        writeln!(&mut fptr, "{}", result).ok();
    }
    
  • + 0 comments

    C#

    public static string timeConversion(string s)
    {
    		DateTime dt = DateTime.Parse(s);
    		return dt.ToString("HH:mm:ss");
    }