We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Time Conversion
- Discussions
Time Conversion
Time Conversion
Sort by
recency
|
848 Discussions
|
Please Login in order to post a comment
//Language = C++ //Approach
Extract the A or P from the given time string. A and P lies at second last index of string. string.size() - 2. Remove AM/PM from the given string.
If the time is given in AM (Then only 12 AM will need to be addressed)
If time is given in PM - Then there are two cases: i. When time is from 01 PM - 11 PM (Simply add 12 in the hrs) ii. When time is 12 PM (Skip this time as it is already in 24 hrs format)
string timeConversion(string s) { int n = s.size(); bool isAM = (s.substr(n - 2, 1) == "A") ? true : false; s = s.substr(0, n-2);
}
Split the time and AM/PM into two variables initally. Foucus on implementing logic to handle the two examples that are provided!
java
My code of JS/TS
import java.io.* import java.math.* import java.security.* import java.text.* import java.util.* import java.util.concurrent.* import java.util.function.* import java.util.regex.* import java.util.stream.* import kotlin.collections.* import kotlin.comparisons.* import kotlin.io.* import kotlin.jvm.* import kotlin.jvm.functions.* import kotlin.jvm.internal.* import kotlin.ranges.* import kotlin.sequences.* import kotlin.text.*
/* * Complete the 'timeConversion' function below. * * The function is expected to return a STRING. * The function accepts STRING s as parameter. */
fun timeConversion(s: String): String { var ret ="" // Write your code here if(s.contains("PM")){ var time = s.split(":")
} else{ var timeWithoutAm = s.split("AM") var cal =timeWithoutAm[0] var time =cal.toString().split(":") var zero = time[0] if(time[0].toInt()>=12){ zero = ((zero.toInt())-12).toString() if(zero.toInt()==0){ zero="00" } }
} return ret }
fun main(args: Array) { val s = readLine()!!
}