Search code examples
c#twitchuptime

#c Twitch bot uptime displaying unwanted milliseconds


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);
}

Solution

  • 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));