0

For the past day or so I've been writing a twitch bot for my channel. Before this I had no knowledge of C# so it could just be a simple fix, but I haven't found any solutions anywhere online.

The problem is that when I use !Uptime it displays the time like so, 01:20:36:1347242 (HH:mm:ss:mmmmmmm)

How would I go about removing the milliseconds when the command is run?

public static readonly DateTime Uptime = DateTime.Now;

if (message.Contains("!uptime"))
{
    int result = Environment.TickCount & Int32.MaxValue;
    var uptime = DateTime.Now - Program.Uptime;
    var minutes = uptime.TotalMinutes;
    irc.sendChatMessage("Stream uptime: " + uptime);
}
krlzlx
  • 5,752
  • 14
  • 47
  • 55
Kronia
  • 3
  • 1
  • uptime.ToString("HH:mm:ss") – Darren Kopp Mar 09 '16 at 05:54
  • nope. don't need minutes variable either, unless you start using it. – Darren Kopp Mar 09 '16 at 05:56
  • I removed the int result & var minutes lines, and added ToString("HH:mm:ss") after the uptime, but when I run it it says format exception was unhandled: An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll Additional information: Input string was not in a correct format. Like I said I only started learning yesterday so its probably an easy fix for someone with more knowledge – Kronia Mar 09 '16 at 06:09

1 Answers1

0

Just use the format string to display only hours, minutes and seconds:

irc.sendChatMessage($"Stream uptime: {uptime:hh\\:mm\\:ss}");

If you are not using C#6, use string.Format:

irc.sendChatMessage(string.Format("Stream uptime: {0:hh\\:mm\\:ss}", uptime));
wertzui
  • 5,148
  • 3
  • 31
  • 51