Skip to content

Instantly share code, notes, and snippets.

@saifullah-azmi
Last active February 14, 2016 14:51
Show Gist options
  • Save saifullah-azmi/7cc413648d964f2dc3a9 to your computer and use it in GitHub Desktop.
Save saifullah-azmi/7cc413648d964f2dc3a9 to your computer and use it in GitHub Desktop.
C# code to convert given whole numbers of seconds to hh:mm:ss
using System;
public class SecondsToTime {
public static int Main() {
double totalSeconds;
Console.Write("Please enter the number of seconds: ");
string userInput = Console.ReadLine();
while (!double.TryParse(userInput, out totalSeconds)) {
if (userInput.Equals("quit")) {
Console.WriteLine("Exiting the programme.");
Environment.Exit(0);
} else {
Console.WriteLine("Invalid Input. Please enter a whole number");
userInput = Console.ReadLine();
}
}
TimeSpan t = TimeSpan.FromSeconds(totalSeconds);
string time = t.ToString(@"hh\h\:mm\m\:ss\s");
Console.Write(totalSeconds + " seconds = " + time);
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment