Reputation: 16196
I need to parse string to DateTime. The string is always in the following format
"10.10.2010" That means dd.MM.yyyy, separated with dots.
I want to use DateTime.TryParse or any other method. Please suggest.
UPDATE
Updated the question. I am just looking for the right method to achieve the goal. Not manual parsing
Upvotes: 31
Views: 59650
Reputation: 424
I have found that jquery datepicker will add non-printable characters in the string. So, when you try to convert to another format, it will throw an invalid date error every time. In my case, I was just trying to convert it back to a time stamp from whatever culture the user was in at the time. It's a somewhat hacky approach, but it worked for me.
static public string ToDigitsOnly(string input)
{
Regex digitsOnly = new Regex(@"[^\d]");
return digitsOnly.Replace(input, "");
}
static private DateTime ConvertDateTimeToDate(string dateTimeString, String langCulture)
{
System.DateTime result;
string[] dateString = dateTimeString.Split('/');
try
{
if (langCulture != "en")
{
int Year = Convert.ToInt32(ToDigitsOnly(dateString[2]));
int Month = Convert.ToInt32(ToDigitsOnly(dateString[1]));
int Day = Convert.ToInt32(ToDigitsOnly(dateString[0]));
result = new DateTime(Year, Month, Day, 00, 00, 00);
}
else
{
int Year = Convert.ToInt32(dateString[2]);
int Month = Convert.ToInt32(dateString[0]);
int Day = Convert.ToInt32(dateString[1]);
result = new DateTime(Year, Month, Day, 00, 00, 00);
}
}
catch
{
// last attempt
result = Convert.ToDateTime(dateTimeString, CultureInfo.GetCultureInfo("en-US"));
}
return result;
}
Upvotes: 0
Reputation: 51
Brazil codec
public static bool IsDateTime(string txtDate)
{
DateTime tempDate;
return DateTime.TryParseExact(txtDate,"dd/MM/yyyy",
new CultureInfo("pt-BR"),
DateTimeStyles.None, out tempDate);
}
Upvotes: 5
Reputation: 269298
Use the TryParseExact
method:
DateTime parsed;
if (DateTime.TryParseExact(yourString, "dd'.'MM'.'yyyy",
CultureInfo.CurrentCulture, DateTimeStyles.None, out parsed))
{
// success
}
Upvotes: 9
Reputation: 120917
Why not use ParseExact
instead?
var theDate = DateTime.ParseExact("dd.MM.yyyy", yourDateString, CultureInfo.InvariantCulture);
Upvotes: 5
Reputation: 1499860
TryParse
doesn't let you specify the format - but you can use TryParseExact
:
DateTime date;
if (DateTime.TryParseExact(text, "dd'.'MM'.'yyyy",
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out date))
{
// Success
}
else
{
// Parse failed
}
Note that the dot doesn't strictly need to be escaped with the quotes, but personally I like to put any literal text in quotes just to make sure that it won't be changed. It's a personal preference thing though - you could certainly just use "dd.MM.yyyy" if you wanted.
Likewise I've specified the invariant culture which is what I normally do for a fixed custom style - but you could make it use the current culture or a specific other culture if you wanted. When you're using a custom style (rather than a standard style such as "long date") it's less likely to make any difference, admittedly.
Upvotes: 67