1

I have this problem:

blTimer.Text = (TimeSpan.FromMinutes(60) - (DateTime.Now - startTime))
              .ToString("hh\\:mm\\:ss");

At this point:

ToString("hh\\:mm\\:ss"); 

Visual Studio shows me the error in the title. How I can resolve?

ThatGuuy
  • 33
  • 4
  • You should always *COPY/PASTE THE EXACT ERROR MESSAGE*! SUGGESTION: change your format string, e.g. `ToString("hh:mm:ss");` or `ToString("hh/mm/ss");`. The backslash ("\") typically indicates an "escape sequence": https://learn.microsoft.com/en-us/cpp/c-language/escape-sequences?view=msvc-160 – paulsm4 Oct 18 '21 at 17:43
  • What is `startTime`? – Klaus Gütter Oct 18 '21 at 17:43
  • @KlausGütter it's this: var startTime = DateTime.Now; var timer = new Timer() { Interval = 1000 } – ThatGuuy Oct 18 '21 at 17:59
  • 1
    What is the .NET version/platform of your project? The `TimeSpan.ToString(string format)` was introduced in .NET Framework 4.0. Don't mix .NET Framework 2.0, .Net Standard 2.0 and .NET Core 2.0. – G Wimpassinger Oct 18 '21 at 19:28

4 Answers4

2

In the future, you should always copy/paste THE FULL, EXACT ERROR MESSAGE into the body of your question!

In this case, please consider changing your format string, e.g. .ToString("hh:mm:ss").

The backslash ("\") typically indicates an "escape sequence": https://learn.microsoft.com/en-us/cpp/c-language/escape-sequences?view=msvc-160

See also How do I write a backslash (\) in a string?


ADDENDUM:

There are a few things that could be going wrong, including: a) the errant backslash in your format string, b) applying the format string to the wrong data type, c) other?

Since it sounds like you've fixed the backslash, I'm guessing maybe the current problem is that you're invoking .ToString() on the wrong data type (a type that ISN'T a System.DateTime object).

SUGGESTIONS:

  • When in doubt, one powerful troubleshooting technique is to split your "complex expression" into "individual statements".
  • Another powerful technique is to write a "Short, Self Contained, Correct (Compilable), Example" - an SSCCE

Here is an example:

using System;

/*
 * SAMPLE OUTPUT:
 * endTime: 12:00:00
 */
namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            DateTime startTime = DateTime.Parse("10/18/21 11:00:00");
            DateTime endTime = startTime + TimeSpan.FromMinutes(60);
            string s = endTime.ToString("hh:mm:ss");
            Console.WriteLine("endTime: {0}", s);
        }
    }
}

I hope this helps point you in the right direction...

G Wimpassinger
  • 751
  • 5
  • 18
paulsm4
  • 114,292
  • 17
  • 138
  • 190
2

RESOLVED :D

The problem was the framework version when i created the project i didn't saw the version of .net so that was the problem! Thanks everybody for helping me!

@paulsm4 - Yep. Thank you, that code is useful for me on some other projects thanks!

ThatGuuy
  • 33
  • 4
0

if startTime is of type DateTime there is nothing wrong. TimeSpan has an overload ToString(string format) and your format works. Tested in PowerShell.

Hazrelle
  • 758
  • 5
  • 9
0

This is the code:

timer.Tick += (obj, args) =>
        lblTimer.Text =
        (TimeSpan.FromMinutes(60) - (DateTime.Now - startTime)).ToString("hh/mm/ss"); //here is the problem on .ToString

        timer.Enabled = true;
        tmr_hide.Start();
        tmr_show.Start();
        tmr_if.Start();
        tmr_himmi.Start();
        tmr_clock.Start();
ThatGuuy
  • 33
  • 4
  • 2
    Please, instead of posting answers to your question, incorporate your updates directly in your question by editing it. – G Wimpassinger Oct 18 '21 at 19:35
  • Great answer! Future answers could be improved by explaining what change you made in the code you posted, and why this works! Feel free to link to documentation as well. See other answers on this question for examples. – Michael Kintscher they-them Oct 18 '21 at 20:15
  • 2
    @MichaelKintscherthey-them: I believe this is not an answer to his own question, it seems to be more a response to Panagiotis Kanavos comment in the answer from Hazrelle. As you can see in the comment (_here is the problem_) on the right of the `ToString` method call. – G Wimpassinger Oct 18 '21 at 20:20