Time Conversion

  • + 0 comments

    GO

    1. Easy to understand. Using "time" package
    func timeConversion(s string) string {
        milLayout := "15:04:05"
        amPmLayout := "03:04:05PM"
        
        //handle err if u need
        t, _ := time.Parse(amPmLayout, s)
        
        return t.Format(milLayout)
    }
    
    1. algo
    func timeConversion(s string) string {
        out := []rune(s)
    
        var isPm int
        if s[len(out)-2:] == "PM" {
            isPm = 1
        } else {
            isPm = 0
        }
    
        //handle err if u need
        hours, _ := strconv.Atoi(s[:2])
        if isPm == 1 && hours != 12 {
            hours += 12
        } else if isPm == 0 && hours == 12 {
            hours = 0
        }
    
        out[0] = rune(hours/10 + '0')
        out[1] = rune(hours%10 + '0')
    
        out = out[:len(out)-2]
    
        return string(out)
    }