Skip to content

Instantly share code, notes, and snippets.

@glebov21
Created February 21, 2018 15:54
Show Gist options
  • Save glebov21/6a7a2427cadbac8bbc14f5e8dc29705a to your computer and use it in GitHub Desktop.
Save glebov21/6a7a2427cadbac8bbc14f5e8dc29705a to your computer and use it in GitHub Desktop.
time left string
public static string GetTimeLeftString(this TimeSpan timeLeft)
{
var ts = timeLeft;
if (ts.TotalSeconds < 0)
return "...";
var result = string.Format("{0}:{1}", ts.Minutes.ToString("00"), ts.Seconds.ToString("00"));
if (timeLeft.TotalHours > 1d)
result = ts.Hours.ToString("00") + ":" + result;
if (timeLeft.TotalDays > 1d)
result = Mathf.FloorToInt((float)ts.TotalDays) + " " + UnityUtils.Localize("Common.Days") + " " + result;
return result;
}
public static string GetTimeLeftString(this double secondsLeft)
{
var ts = DateTime.Now.AddSeconds(secondsLeft) - DateTime.Now;
return GetTimeLeftString(ts);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment