DateTime TryParseExact a string containing a 3 letter month - c#

I am writing an extension method to parse a specific string which contains a date and a time into a DateTime object using the DateTime.TryParseExact() Method.
An example of the format is as follows:
"29 November 2013 20:04"
The code I am using to parse it to a DateTime is:
public static DateTime MyToDateTime(this string value)
{
DateTime converted;
DateTime.TryParseExact(value, "dd MMM yyyy hh:mm", CultureInfo.InvariantCulture, DateTimeStyles.AllowWhiteSpaces, out converted);
return converted;
}
The result is always DateTime.Min (i.e 0001-01-01 00:00:00.000)
I cant figure out what is wrong with my format string. Any help would be appreciated.

from your comments:
if you want to parse 3 Letter Month use MMM.
if you want to parse 24-Hour format you should use HH instead of hh.
Try This:
DateTime.TryParseExact(value, "dd MMM yyyy HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.AllowWhiteSpaces, out converted);

There are two problems I see:
November is not a 3-letter month—that would be Nov. To parse a full date name, use MMMM.
To parse a 24-hour time use HH.
This should work:
DateTime.TryParseExact(value, "dd MMMM yyyy HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.AllowWhiteSpaces, out converted);
Further Reading
Custom Date and Time Format Strings

Try adding an extra M and a capital H
DateTime.TryParseExact(value, "dd MMMM yyyy H:mm", .....
See here for more info: How can I visualize the way various DateTime formats will display?

MMM stands for the abbreviated name of the month, so it's not what you're looking fore. Use MMMM instead.
Find all custom Date and Time format string on MSDN: Custom Date and Time Format Strings.
You should also check the value returned by TryParseExact method. It returns false when parse failed and true when it was performed without any problems.
And hh should be HH to parse hour part of your input.

Related

DateTime.ParseExact without a delimiter [duplicate]

Hi I have the following method and I'm passing it the value "07 Jan 2014 13:48:46" and from what I understand the TryParseExact should be matching the format "dd MMM yyyy hh:mm:ss" and returning true, however it is returning false, any ideas?
string[] formats= {"dd-MM-yyyy hh:mm:ss",
"dd MMM yyyy hh:mm:ss",
"dd MMM yyyy",
"hh-mm-ss",
"dd-MM-yyyy",
"dd-MM-yy",
};
DateTime result;
if (DateTime.TryParseExact(value, formats, CultureInfo.CurrentCulture, DateTimeStyles.None, out result))
{
return result;
}
return null;
24-hour time requires use of HH, not hh. Lowercase h is for 12 hour time.
See: http://msdn.microsoft.com/en-us/library/8kb3ddd4%28v=vs.110%29.aspx
Because none of your format have 24-hour clock.
hh specifier is for 01 to 12. It doesn't have 13 as an hour.
Use HH specifier instead which is for 00 to 23.
For more information, take a look at;
Custom Date and Time Format Strings

String to date conversion in c# as yyyy/mm/dd

I would like convert string content to date format as yyyy/MM/dd HH:mm:ss tt
string date = "2014-11-20 3:21:00 PM";
DateTime date_=System.DateTime.Now;
var result = DateTime.TryParseExact(date, "yyyy-MM-dd HH:mm:ss tt",
System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.None,
out date_);
But it returns result doesn't match requirement also TryParse function return false. If time zone is not defined, it will return expected result.
HH specifier is for 24 hour clock which is 00 to 23.
You need to use h specifier instead which represents 1 to 12 in 12 hour clock.
Also you don't need to initialize your out parameter value. Definition will be enough like;
string date = "2014-11-20 3:21:00 PM";
DateTime date_;
var result = DateTime.TryParseExact(date, "yyyy-MM-dd h:mm:ss tt",
CultureInfo.InvariantCulture,
DateTimeStyles.None, out date_);
The DateTime data type format is always MM/dd/yyyy hh:mm:ss tt (i.e. Date = {11/20/2014 12:00:00 AM}),
If you want to display the value you can change the format by using ToString extention method

DateTime.TryParseExact not working with expected string

Hi I have the following method and I'm passing it the value "07 Jan 2014 13:48:46" and from what I understand the TryParseExact should be matching the format "dd MMM yyyy hh:mm:ss" and returning true, however it is returning false, any ideas?
string[] formats= {"dd-MM-yyyy hh:mm:ss",
"dd MMM yyyy hh:mm:ss",
"dd MMM yyyy",
"hh-mm-ss",
"dd-MM-yyyy",
"dd-MM-yy",
};
DateTime result;
if (DateTime.TryParseExact(value, formats, CultureInfo.CurrentCulture, DateTimeStyles.None, out result))
{
return result;
}
return null;
24-hour time requires use of HH, not hh. Lowercase h is for 12 hour time.
See: http://msdn.microsoft.com/en-us/library/8kb3ddd4%28v=vs.110%29.aspx
Because none of your format have 24-hour clock.
hh specifier is for 01 to 12. It doesn't have 13 as an hour.
Use HH specifier instead which is for 00 to 23.
For more information, take a look at;
Custom Date and Time Format Strings

C# DateTime Conversion from yyyy-MM-ddTHH:mm:ss to dd MMM yyyy

How to convert "yyyy-MM-ddTHH:mm:ss" into "dd MMM yyyy" format? For Instance, i want to convert 2013-04-16 05:30:05 into 16 April 2013. What is the correct method to achieve this?
First ParseExact then do ToString (I assume that you have string object, if you have DateTime object, skip first line)
var dateTime = DateTime.ParseExact(yourDateString, "yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture);
var yourNewString = dateTime.ToString("dd MMM yyyy");
Note that representation of DateTime you see in debugger is dependant on your current culture.
First, a DateTime has no format. But if you've already a string that represents a DateTime with the format yyyy-MM-ddTHH:mm:ss and you want to convert it to a string-date with format dd MMM yyyy you need to parse it to DateTime first.
Therefore use DateTime.ParseExact:
DateTime dt = DateTime.ParseExact("2013-04-16 05:30:05", "yyyy-MM-dd HH:mm:ss", null);
Now you can use DateTime.ToString:
string result = dt.ToString("dd MMM yyyy");
Note that you need to pass a different CultureInfo object to ParseExact/ToString if you want to parse with another DateTimeFormat than your current (f.e. force english month names instead of german: dt.ToString("dd MMM yyyy", CultureInfo.InvariantCulture)).
As others mentioned, a DateTime has no format. To parse a string literal to a Date you need to call DateTime.Parse (if the string is in a culture-specific format) or DateTime.ParseExact if you need to pass a format string.
The format can be a custom format like yyyy-MM-dd HH:mm:ss or one of the standard format strings, eg. s for yyyy-MM-ddTHH:mm:ss.
2013-04-16 05:30:05 it not in one of the standard formats, so you have to parse by passing a custom format string:
var dt = DateTime.ParseExact("2013-04-16 05:30:05", "yyyy-MM-dd HH:mm:ss", null);
On the other hand, yyyy-MM-ddTHH:mm:ss is the s standard format so you can just write:
var dt = DateTime.ParseExact("2013-04-16T05:30:05", "s", null);

C# DateTime FormatException for dd MMM yyyy

I am having a string "10-Dec-2013". I just want to convert it into Datetime type. The format should be "dd MMM yyyy".
I used the following code.
DateTime watchDate = DateTime.ParseExact("18-Nov-2013", "dd MMM yyyy", CultureInfo.CurrentCulture);
But i am getting FormatException. Where am i going wrong?
You need the dashes:
DateTime watchDate = DateTime.ParseExact("18-Nov-2013", "dd-MMM-yyyy", CultureInfo.InvariantCulture);
// ^ ^
The format has to match your input.. which contains dashes.
If you want to re-store it in a string (not sure why you want to do that, since you already have the string you want) then you need to format it again:
string date = watchDate.ToString("dd-MMM-yyyy");

Categories

Resources