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?
CodePudding user response:
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://docs.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...
CodePudding user response:
if startTime
is of type DateTime
there is nothing wrong. TimeSpan
has an overload ToString(string format)
and your format works. Tested in PowerShell.
CodePudding user response:
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();
CodePudding user response:
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!