Ignoring zero in DateTime.ParseExact with IT-it culture - c#

I'm parsing this date: "3/1/1961" with the following code:
DateTime d = DateTime.ParseExact(theStringDate, "d", lang.Culture);
I think everybody will agree with me that "3/1/1961" and "03/01/1961" are the same date. However, in the first case the code will crash and in the second it will not.
Why is C# behaving this way? Is there any way "3/1/1961" should not get interpreted correctly? How can I tell the compiler to ignore the absence of 0 before the number?
Forcing my user to write 0 before every number or using JS to force the presence of a 0 are both unacceptable solutions. What can I do?
What is the value of lang.Culture?
When the problem arised, I was testing from an italian browser so, I guess, it is "IT-it".

I am not sure how you are parsing with the current format d, it should be "d/M/yyyy"
string theStringDate = "03/01/1961";
DateTime d = DateTime.ParseExact(theStringDate, "d/M/yyyy", CultureInfo.InvariantCulture);
The format "d/M/yyyy" works for both single digit and double digits day/month.

You're using parse exact and the input string isn't in the format you specify. C# is not behaving weird, you're just misusing the method. This snippet is directly from msdn and exactly explains why you get an exception;
// Parse date-only value without leading zero in month using "d" format.
// Should throw a FormatException because standard short date pattern of
// invariant culture requires two-digit month.
dateString = "6/15/2008";
try {
result = DateTime.ParseExact(dateString, format, provider);
Console.WriteLine("{0} converts to {1}.", dateString, result.ToString());
}
catch (FormatException) {
Console.WriteLine("{0} is not in the correct format.", dateString);
}
There are several solutions available to you. Here are few off the top of my head; change your format specifier (I think replacing "d" with "g" will solve your problem in this case, you'll still get exceptions for other formats though), change DateTime.ParseExact to DateTime.Parse or DateTime.TryParse, or change your input so it's in the exact format you require.
Personally I'd recommend getting rid of ParseExact. Do you know exactly what format your date is going to be in? It seems like you don't. If you don't, why would you be using ParseExact?

Related

DateTime.Parse with custom formats

I'm trying to use DateTime.Parse in order to parse a string containing a DateTime in a custom format.
The format is yy-MMM MMMM-dddd-ddd-dd, the string is 15-jan. január-szerda-Sze-07.
I've modified the ShortDatePattern in the OS' regional settings, and you can see it in the CultureInfo.CurrentCulture while debugging.
I'm using the following code:
var date = DateTime.Parse(dateInString, CultureInfo.CurrentCulture);
But it fails with exception String was not recognized as a valid DateTime.
Using ParseExact it does work.
var date = DateTime.ParseExact(
dateInString,
CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern,
CultureInfo.CurrentCulture);
Shouldn't Parse work as well?
Any help would be appreciated.
Edit:
Assuming Parse is just not good enough, should this be okay or can it cause problem I can't think of right now (it works with the aforementioned problem)?
DateTime date = new DateTime();
bool success = false;
foreach (var format in currentCulture.DateTimeFormat.GetAllDateTimePatterns())
{
success = DateTime.TryParseExact(dateString, format, culture, DateTimeStyles.AllowWhiteSpaces, out date);
if (success)
break;
}
if (!success)
throw new Exception();
I played a little bit with your example and I couldn't make it work. I think that DateTime.Parse is simply not clever enough to parse your string and MSDN documentation confirms that. Here is a section from ParseExact documentation according to which Parse should not be used with custom cultures/patterns and why:
If you parse a date and time string generated for a custom culture, use the ParseExact method instead of the Parse method to improve the
probability that the parse operation will succeed. A custom culture
date and time string can be complicated, and therefore difficult to
parse. The Parse method attempts to parse a string with several
implicit parse patterns, all of which might fail.
And here is another interesting fragment from SetAllDateTimePatterns documentation:
The Parse and TryParse methods do not fully iterate all strings in patterns when parsing the string representation of a date and time. If
you require a date and time string to have particular formats in a
parsing operation, you should pass the array of valid formats to the
DateTime.ParseExact...

TryParseExact doesn't receive valid format c#

I am trying to input a datetime value from the console. However, The TryParseExact method doesn't receive valid formats:
string startdate;
DateTime inputDate;
while (true)
{
Console.WriteLine("Input time [HH:MM TT]");
startdate = Console.ReadLine();
if (DateTime.TryParseExact(startdate, "hh:mm tt",
CultureInfo.CurrentCulture,
DateTimeStyles.None, out inputDate))
{
break;
}
}
Any suggestions?
If it is not accepting input that you think is valid then there are two possible causes of the problem:
1) The input is not what you think it is. This could be caused by copying and pasting from somewhere else that is including non-valid characters or something. IF you are manually typing the number then this is unlikely to be an issue.
2) The format you are accepting is not what you think it is. This is most likely since there are some subtleties that can bite you on this. The key think is to look up everything in http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx and be aware that several elements you are using are culture dependant. You are obviously at least partially aware of cultures since you are explicitly stating you want the current culture. Without knowing what that culture is though its hard to say what the input should be.
A case in point is that in your format : doesn't mean a literal colon but the "Time Separator". To quote the MSDN page: "The ":" custom format specifier represents the time separator, which is used to differentiate hours, minutes, and seconds. The appropriate localized time separator is retrieved from the DateTimeFormatInfo.TimeSeparator property of the current or specified culture."
As you can see this means that it is not always :.
Often the best fix, especially since you are hard definining the format, is to use CultureInfo.InvariantCulture which will guarantee not to change depending on where you run the software, etc. Otherwise you should generate the string that specifies the correct input using the relevant components of your current culture object.
An example of how to write this without the white(true) loop / to try to make it easier for your users:
string startdate;
DateTime inputDate;
while (inputDate == null)
{
Console.WriteLine("Input time [HH:MM TT]");
startdate = Console.ReadLine();
if (!DateTime.TryParseExact
(
startdate, "hh:mm tt"
,CultureInfo.CurrentCulture
,DateTimeStyles.None
, out inputDate
))
{
Console.WriteLine(String.Format("'{0}' is an invalid value."));
//http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx
Console.WriteLine(
String.Format("Example: The time now is '{0}'"
,DateTime.Now.ToString("hh:mm tt", CultureInfo.CurrentCulture))
);
}
}
Console.WriteLine("That's a valid time :)");
NB: your above code works in so much as given the correct input (for your user's current culture) the code exits the loop.

TimeSpan.TryParseExact not working

I'm trying to retrieve a timespan from a string, but TryParseExact is returning false (fail).
I can't see what I'm doing wrong, can you help? I've tried 2 versions of my line in the code, both do not work.
TimeSpan.TryParseExact("04:00:01","HH:mm:ss",CultureInfo.CurrentCulture, out aTime)
and
TimeSpan.TryParseExact("04:00:01","HH:mm:ss", null, out aTime)
EDIT:
both responses here are correct, I have the wrong format for my custom timespan format - the mistake I made is to assume that the custom formats for DateTime would work for TimeSpans, but they do not.
The problem is simply in the format string for the TimeSpan, you have specified "HH:mm:ss". The specifier HH (upper case) is not valid for timespan. You should use hh. Format strings are indeed case sensitive.
The colon character (:) also needs to be escaped, so use "hh\\:mm\\:ss", #"hh\:mm\:ss" or "hh':'mm':'ss". All three forms will have the same effect.
You can review a list of valid custom format strings for TimeSpan here. and the standard format strings for TimeSpan are here.
While HH is valid for DateTime and DateTimeOffset where it represents the 24 hour clock and lower case hh represents a 12 hour clock, For TimeSpan - the hours component is always based on 24 hours. You would think that the HH format would be the one chosen, for uniformity, but nope - it's hh.
It's probably should get mentioned that you need to escape the colon character.
TryParseExact("04:00:01", "HH\\:mm\\:ss" ...
The string format which you are passing is wrong.
var res=TimeSpan.TryParseExact("04:00:01", "g", CultureInfo.CurrentCulture, out aTime);
g- General short format and is culture sensitive.
More on this here Standard Timespan Format Strings
Maybe you were using multiple formats.
public const string TimeFormat1 = "hh\\:mm";
public const string TimeFormat2 = "hh\\:mm:\\ss";
var parsed = TimeSpan.TryParseExact(time, new [] { TimeFormat1, TimeFormat2 }, CultureInfo.CurrentCulture, out TimeSpan ts1);
// parsed is always false
You might have thought you escaped your colon; but didn't, actually...
This "hh\\:mm:\\ss" won't work.
Using TimeFormat2 in ParseExact throws a FormatException...
You meant to use this "hh\\:mm\\:ss" instead.

datetime.parseexact returns wrong month

Here is my code:
a.dateFrom = DateTime.ParseExact(x, "dd/mm/yyyy", null);
And x has value of: 08/03/2012
However, a.dateFrom has value of 08/01/2012. Why?
You should use MM as format for month
As ionden notes, you should have a format of
"dd/MM/yyyy"
Currently you're parsing the second part as minutes (as that's what mm means).
See the documentation for custom date and time format strings for more information. I'd also strongly encourage you to consider using the invariant culture for parsing - if you're using a custom format string, that usually means you don't want to treat the input in a culture-sensitive fashion at all.

DateTime.TryParseExact not working as expected

Can anyone explain why the following snippet returns true?
According to the docs for The "d" custom format specifier, "A single-digit day is formatted without a leading zero." So why doesn't TryParseExact fail when I give it a single-digit day with a leading zero?
DateTime x;
return DateTime.TryParseExact
(
"01/01/2001",
#"d\/MM\/yyyy",
null,
System.Globalization.DateTimeStyles.None,
out x
);
UPDATE
I think maybe I was unclear originally. What I am really trying to get at is: Why does TryParseExact accept some values that don't match exactly? from all of the documentation I have seen, 'd' matching '01' and '1' is just as much a bug as if 'MM' matched 'March' as well as '03'. The issue here isn't that the values are equivalent, its that they don't match the format.
The relevant snippets of documentation are:
From TryParseExact: The format of the string representation must match a specified format exactly.
From The 'd' Specifier: A single-digit day is formatted without a leading zero.
It seems abundantly clear to me that '01' has a leading 0, and therefore doesn't exactly match 'd'.
From the .NET 4 source in DateTimeParse.ParseByFormat():
case 'd':
// Day & Day of week
tokenLen = format.GetRepeatCount();
if (tokenLen <= 2) {
// "d" & "dd"
if (!ParseDigits(ref str, tokenLen, out tempDay)) {
if (!parseInfo.fCustomNumberParser ||
!parseInfo.parseNumberDelegate(ref str, tokenLen, out tempDay)) {
result.SetFailure(ParseFailureKind.Format, "Format_BadDateTime", null);
return (false);
}
}
if (!CheckNewValue(ref result.Day, tempDay, ch, ref result)) {
return (false);
}
}
else
{...}
The parser lumps "d" and "dd" together.
It appears that behavior is by design, and I think it works that way to be consistent with other string formatting options.
Take the following example:
//Convert DateTime to string
string dateFormat = "d/MM/yyyy";
string date1 = new DateTime(2008, 10, 5).ToString(dateFormat);
string date2 = new DateTime(2008, 10, 12).ToString(dateFormat);
//Convert back to DateTime
DateTime x1, x2;
DateTime.TryParseExact(date1, dateFormat, null, System.Globalization.DateTimeStyles.None, out x1);
DateTime.TryParseExact(date2, dateFormat, null, System.Globalization.DateTimeStyles.None, out x2);
Console.WriteLine(x1);
Console.WriteLine(x2);
In the first part, ToString() outputs a two digit day for October 12th, because it wouldn't make much sense to just write out a single digit day (and which digit would it pick, the 1 or the 2?). So since the "d" represents one OR two digit days when converting to a string, it would have to work the same way when converting back to DateTime. If it didn't, the conversion back to DateTime in TryParseExact in my example would fail, and that would definitely not be an expected behavior.
I would say that if you really need to match a d/MM/yyyy format exactly, you could probably use a regex to validate the string and then pass it through Parse, TryParse or TryParseExact (depending on how good your regex is, since it would have to handle leap years, 30/31 days, etc if you wanted to use Parse).
I'd say it doesn't fail because TryParseExact is smart enough to know that '01' == '1'.
TryParseExact is just trying to be flexible in this case I guess. But the "d" vs "dd" should and would work as advertised when you are converting date to string using a format specifier.
Because a single 'd' means that your DateTime value will be converted to as short value as possible, i.e. without leading zero if there's no necessity for it. I suppose it shouldn't fail when you're converting from string to DateTime because the main purpose of TryParseExact's format string is to help to convert to DateTime, i.e. it serves like a hint, it's not intended to validate string format.
You can use RegEx if you still need hardcore string format validation.

Categories

Resources