Answer the question
In order to leave comments, you need to log in
String to DateTime and calculate percentage, how to implement in C#?
Hello.
There is a line "1:15". Which means - one minute 15 seconds. Or the string could be "1:15:20", which means one hour, fifteen minutes, twenty seconds. How can I convert such a string to DateTime, then calculate the percentage of time and return the result to DateTime.
For example, you need to calculate 40% of the time from 2:15:10 (two hours, fifteen minutes, ten seconds)
Answer the question
In order to leave comments, you need to log in
public static string getTimePercent(string time,float percent){
return TimeSpan.FromSeconds(TimeSpan.Parse(time).TotalSeconds * (percent / 100f)).ToString(@"hh\:mm\:ss");
}
var diff = getTimePercent("2:15:00",40);//00:48:00
TimeSpan and its methods, I don’t remember exactly if there are percentages there, but it is converted to long, and there you can already create a new one
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question