Time Conversion

Sort by

recency

|

5169 Discussions

|

  • + 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;
        }
    
  • + 0 comments
    function timeConversion(s: string): string {
        const ampm = s.substring(s.length -2, s.length).toUpperCase() as "AM" | "PM";
        let hour: number = parseInt(s.substring(0, 2));
        
        if (Number.isNaN(hour)) throw new Error("Hour is NaN");
        
        if (ampm === "AM" && hour === 12) {
            hour = 0;
        } else if (ampm === "PM" && hour < 12) {
            hour += 12;
        }
        
        return `${String(hour).padStart(2, "0")}${s.substring(2, s.length - 2)}`;
    }