Answer the question
In order to leave comments, you need to log in
How to convert AM/PM time to 24h format in golang?
I'm trying to solve the problem
https://www.hackerrank.com/challenges/time-convers...
Code:
func timeConversion(s string) string {
format := s[len(s)-2:]
strHour := s[:2]
hour, err := strconv.Atoi(strHour)
if err != nil {
fmt.Println(err)
}
if format == "AM" && hour == 12 {
return "00" + s[2:len(s)-2]
}
if format == "PM" && hour == 12 {
return "12" + s[2:len(s)-2]
}
if format == "PM" {
hour = hour + 12
}
return strconv.Itoa(hour) + s[2:len(s)-2]
}
This code have 2/10 test cases failed :(
Answer the question
In order to leave comments, you need to log in
Midnight is 12:00:00AM on a 12-hour clock, and 00:00:00 on a 24-hour clock.
Noon is 12:00:00PM on a 12-hour clock, and 12:00:00 on a 24-hour clock.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question