public DateTime (int year, int month, int day, int hour, int minute, int second);
string
in the first place then you should do suggested above. If you're starting with that string
though, e.g. reading it from a file, then you should call DateTime.TryParseExact
. You get to specify the format the input is in and the method will return a bool
to indicate whether it succeeded or failed and, if it succeeded, will output the DateTime
value. E.g.var bufferDateTime = "2020,07,22,16,11,38";
if (DateTime.TryParseExact(bufferDateTime, "yyyy,MM,dd,HH,mm,ss", null, DateTimeStyles.None, var out value))
{
Console.WriteLine(value);
}
else
{
Console.WriteLine("Invalid input.");
}
Hi, I have added "using System.Globalization";If you're starting with numbers to create thestring
in the first place then you should do suggested above. If you're starting with thatstring
though, e.g. reading it from a file, then you should callDateTime.TryParseExact
. You get to specify the format the input is in and the method will return abool
to indicate whether it succeeded or failed and, if it succeeded, will output theDateTime
value. E.g.
C#:var bufferDateTime = "2020,07,22,16,11,38"; if (DateTime.TryParseExact(bufferDateTime, "yyyy,MM,dd,HH,mm,ss", null, DateTimeStyles.None, var out value)) { Console.WriteLine(value); } else { Console.WriteLine("Invalid input."); }
var out value
should have been out DateTime value
.Sorry, should have beenHi, I have added "using System.Globalization";
There are issues with "var out value".
"," expected
The name var does not exist in current ....
The name value does not ........
Thanks,
out var
rather than var out
. That's what I get for typing that code into the forum freehand instead of into the IDE and then pasting.var out value
should have beenout DateTime value
.
out var
is fine as the DateTime
type will be inferred from usage. I just got the out
and var
the wrong way around, and not for the first time.