Time Conversion

Sort by

recency

|

5170 Discussions

|

  • + 0 comments

    Rust

    fn timeConversion(s: &str) -> String {
        let prefix = &s[8..10];
        let hour_slc = &s[0..2];
        let minutes = &s[3..5];  
        let seconds = &s[6..8]; 
        let hour_num: i32 = hour_slc.parse().unwrap();
        
        match prefix {
            "AM" => {
                if hour_num == 12 {
                   format!("00:{}:{}", minutes, seconds) 
                } else {
                 format!("{:02}:{}:{}", hour_num, minutes, seconds)
                }
            },
            "PM" => {
                if hour_num == 12 {
                   format!("12:{}:{}", minutes, seconds) 
                } else {
                  let convert_hour = hour_num + 12;
                  format!("{:02}:{}:{}", convert_hour, minutes, seconds)
                }
            },
            _ => "00:00:00".to_string()
        }
    }
    
  • + 0 comments

    Can you help me to integrate this time conversion code into my Ninja Arashi game? I am currently working on building an advance version of the game. So users can enjoy every feature of the game to make it realistic.

  • + 0 comments

    python using array slicing:

    def timeConversion(s): newS = "" if "AM" in s and "12" in s: newS = "00" elif "PM" in s and "12" not in str(s[:2]): newS = str(int(s[:2]) + 12) else: return s[:len(s)-2] return newS + s[2:len(s)-2]

  • + 0 comments

    Loving python... from datetime import datetime

    time = datetime.strptime(s, "%I:%M:%S%p").strftime("%H:%M:%S")
        return time
    
  • + 0 comments

    Java 8 based with 2 different approaches

        // Using Java LocalTime class.
        private static String timeConverterLocalTime(String input) {
            DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("hh:mm:ssa");
            DateTimeFormatter outputFormat = DateTimeFormatter.ofPattern("HH:mm:ss");
            LocalTime timeIn24 = LocalTime.parse(input, timeFormatter);
            return timeIn24.format(outputFormat);
        }
    
        // Using Java algorithmic approach.
        private static String timeConverter(String input) {
            String timeIn24 = "";
            String hours = input.substring(0, 2);
            String timeWithoutPrefix = input.substring(0, input.length() - 2);
    
            if (input.contains("PM")) {
                if (hours.equals("12")) {
                    timeIn24 = timeWithoutPrefix;
                } else {
                    int hourIn24 = Integer.parseInt(hours) + 12;
                    timeIn24 = String.valueOf(hourIn24).concat(timeWithoutPrefix.substring(2));
                }
            }
    
            if (input.contains("AM")) {
                if (hours.equals("12")) {
                    timeIn24 = "00".concat(timeWithoutPrefix.substring(2));
                } else {
                    timeIn24 = timeWithoutPrefix;
                }
            }
            return timeIn24;
        }