timeConversion

    0

    0

    In code, we first split the string into two parts, time and part. We then use the index of the PM symbol in the string to change the 12 to 12 PM. If the part of the string is not PM, it is set to the first 12 of the string. We then use the index of the AM symbol in the string to change the 0 to 12 AM. Finally, we join the parts together with a space.

    function timeConversion(s) {
      let [time, part] = [s.substring(0, s.length - 2), s.substring(s.length - 2)]
      time = time.split(":").map(Number)
      if (part === "PM" && time[0] === 12) time[0] = 12
      if (part === "PM" && time[0] !== 12) time[0] = (time[0] + 12) % 24
      if (part === "AM" && time[0] === 12) time[0] = 0
      return time
        .map(String)
        .map(s => s.padStart(2, "0"))
        .join(":")
    }
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.