Skip to content

Instantly share code, notes, and snippets.

@kyrathasoft
Created June 2, 2014 11:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyrathasoft/3e6b808a31fe2a4a5145 to your computer and use it in GitHub Desktop.
Save kyrathasoft/3e6b808a31fe2a4a5145 to your computer and use it in GitHub Desktop.
Takes total elapsed milliseconds and returns a string description of the elapsed time, such as "593 milliseconds, or 4.203 seconds, or 5.111 minutes".
using System;
namespace com.williambryanmiller.time {
class TimeFormatting {
public static string getDurationDesc(double numMilliseconds) {
const double Minute = 60000;
const double Second = 1000;
double duration = 0;
string result = string.Empty;
if (numMilliseconds >= Minute) {
duration = numMilliseconds / Minute;
//result = String.Format("{0:0.00}", duration) + " minutes";
result = duration.ToString("N3") + " minutes";
} else {
if (numMilliseconds >= Second) {
duration = numMilliseconds / Second;
//result = String.Format("{0:00}", duration) + " seconds";
result = duration.ToString("N3") + " seconds";
} else {
result = numMilliseconds.ToString() + " milliseconds";
}
}
return result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment