using System;
using System.Globalization;
namespace ConsoleApplication20
{
class Program
{
static void Main(string[] args)
{
string[] formats ={
"MM/dd/yyyy HH:mm:ss tt",
"MM/dd/yyyy HH:mm:ss",
"MM/dd/yyyy H:mm:ss tt",
"MM/dd/yyyy H:mm:ss",
"M/dd/yyyy HH:mm:ss tt",
"M/dd/yyyy HH:mm:ss",
"M/dd/yyyy H:mm:ss tt",
"M/dd/yyyy H:mm:ss",
"MM/d/yyyy HH:mm:ss tt",
"MM/d/yyyy HH:mm:ss",
"MM/d/yyyy H:mm:ss tt",
"MM/d/yyyy H:mm:ss",
"M/d/yyyy HH:mm:ss tt",
"M/d/yyyy HH:mm:ss",
"M/d/yyyy H:mm:ss tt",
"M/d/yyyy H:mm:ss"
};
string date1 = "11/08/2008 4:00:49";
DateTime result;
if (DateTime.TryParseExact(date1, formats,
new CultureInfo("en-US"),
DateTimeStyles.None,
out result))
{
DateTime startTime = result;
}
Console.W开发者_如何转开发rite(startTime);
}
}
}
please explain me .. why always that if statement returns false ..
Thanx for help and support, Rik,Phillip,Joel and Kent and everyone else ..You've declared startTime within the scope of a different block from which you're printing it out. Try changing to:
if (DateTime.TryParseExact(date1, formats,
new CultureInfo("en-US"),
DateTimeStyles.None,
out result))
{
DateTime startTime = result;
Console.Write(startTime);
}
You should declare (and initialize)
DateTime startTime
Outside the IF statement, or
Console.WriteLine(startTime)
Inside the IF statement.
You can try to change the value of the DateTimeStyles parameter to either AssumeLocal or AssumeUniversal (depending on your needs) to allow for the lack of a time-zone specification.
Kent Boggart nailed your compiler error, but I noticed one other problem with your formats. You're mixing combinations of different month, day, hour, and am/pm formats, but you only use uppercase 'H' for your hour formats. An uppercase 'H' means use 24 hour time, while an lowercase 'h' means use 12 hour time. So you should probably only have the uppercase 'H' on samples where you don't have a 'tt' option, like this:
string[] formats ={
"MM/dd/yyyy hh:mm:ss tt",
"MM/dd/yyyy HH:mm:ss",
....
};
You are declaring and init'n startTime inside of your if. Try
DateTime result;
DateTime startTime = new DateTime;
if (DateTime.TryParseExact(date1, formats,
new CultureInfo("en-US"),
DateTimeStyles.None,
out result))
{
startTime = result;
}
Console.Write(startTime);
Here's a code snippet that works - no compiler error, and the 'if' statement succeeds. Note, that you need to put a Console.ReadKey() at the end of the file in order to keep the cmd window from disappearing too quickly.
using System;
using System.Globalization;
namespace so1888431_timestringparse
{
class Program
{
static void Main(string[] args)
{
string[] formats ={
"MM/dd/yyyy HH:mm:ss tt",
"MM/dd/yyyy HH:mm:ss",
"MM/dd/yyyy H:mm:ss tt",
"MM/dd/yyyy H:mm:ss",
"M/dd/yyyy HH:mm:ss tt",
"M/dd/yyyy HH:mm:ss",
"M/dd/yyyy H:mm:ss tt",
"M/dd/yyyy H:mm:ss",
"MM/d/yyyy HH:mm:ss tt",
"MM/d/yyyy HH:mm:ss",
"MM/d/yyyy H:mm:ss tt",
"MM/d/yyyy H:mm:ss",
"M/d/yyyy HH:mm:ss tt",
"M/d/yyyy HH:mm:ss",
"M/d/yyyy H:mm:ss tt",
"M/d/yyyy H:mm:ss"
};
string date1 = "11/08/2008 4:00:49";
DateTime result;
if (DateTime.TryParseExact(date1, formats,
new CultureInfo("en-US"),
DateTimeStyles.None,
out result))
{
DateTime startTime = result;
Console.Write(startTime);
}
Console.ReadKey();
}
}
}
startTime is only defined in the scope of that if statement move the Console.write into the if statement, or declare the variable above it
The reason for the compiling error is because startTime
goes out of scope.
The way to fix your problem properly though is to print result
instead inside the if.
startTime
is only declared when the result of your if
statement is true.
The Console.Write(startTime)
is outside of this if
and therefore if the if
doesn't return true, startTime
wont exist.
精彩评论