DateTime.ParseExact without a delimiter [duplicate] - c#

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

Related

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

DateTime TryParseExact a string containing a 3 letter month

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.

Error : String was not recognized as a valid DateTime while converting to date format in c#

Here is the date time format i'm trying to format.I'm getting this date format from twitter apis
string date = "Thu Jul 18 17:39:53 +0000 2013"
i tried
Convert.ToDateTime(date).ToString("dd/MM/yyyy")
But it says String was not recognized as a valid DateTime.
This works:
DateTime.ParseExact(dtStr, "ddd MMM dd HH:mm:ss zzzz yyyy", CultureInfo.InvariantCulture)
ParseExact and TryParseExact allows to use a custom format string. ddd is the abbreviated day name, MMM the abbreviated month name, dd the day number, HH hours in 24h clock format, mm minutes, ss seconds, zzzz the time-zone and yyyy the years.
I have used CultureInfo.InvariantCulture to specify that the current culture is not used but InvariantCulture which is similar to "en-US".
Demo
works but after getting date from your line of code i tried to do
date.ToString("dd/mm/yyyy") but get the string as 12-12-2013, no
slashes
/ is a replacement character for your current culture's date-separator which is obviously -. So also use CultureInfo.InvariantCulture to specify that the separator should be used without using your current culture:
string result = dateTime.ToString("dd/mm/yyyy", CultureInfo.InvariantCulture);
See: The "/" Custom Format Specifier
Try this
DateTime.ParseExact(YourDate, "ddd MMM dd HH:mm:ss KKKK yyyy", CultureInfo.InvariantCulture)
Its better to use Invariant culture than Current culture
You are trying to convert a non-standard format, so use this:
string dateStr = "Thu Jul 18 17:39:53 +0000 2013";
DateTime date = DateTime.ParseExact(dateStr, "ddd MMM dd h:mm:ss KKKK yyyy", System.Globalization.CultureInfo.InvariantCulture);
Or build the correct format for your input.
How about like;
string date = "Thu Jul 18 17:39:53 +0000 2013";
DateTime dt = DateTime.ParseExact(date, "ddd MMM dd HH:mm:ss KKKK yyyy", CultureInfo.InvariantCulture);
Console.WriteLine(dt);
Output will be;
18.07.2013 20:39:53
K for time zone information in here.
Check out for more information;
Custom Date and Time Format Strings
Your date string needs to be this:
Thu Jul 18 2013 17:39:53 +0000
Whatever is producing your string needs to have the year value after the month and day and before the time, like above.
string date = "Thu Jul 18 2013 17:39:53 +0000";
var theDate = Convert.ToDateTime(date);
Note: This will produce a valid .NET DateTime object.
UPDATE:
If you cannot change the string produced, then use the ParseExact method with a custom format, like this:
string date = "Thu Jul 18 17:39:53 +0000 2013";
var theDate = DateTime.ParseExact(date, "ddd MMM dd H:mm:ss zzz yyyy", CultureInfo.InvariantCulture);
Try using DateTime.ParseExact.
string date = "Thu Jul 18 17:39:53 +0000 2013"
DateTime date = DateTime.ParseExact(date, "dd/MM/yyyy", null);
this.Text="22/11/2009";
DateTime date = DateTime.ParseExact(this.Text, "dd/MM/yyyy", null);

C# : How to convert string to DateTime, where the string can have any of the standard datetime format

I had posted a question on DateTime to String conversion, I got many satisfying answers for that .. so I thank StackOverflow very much .. Here is one more problem of String manupulation, I am stuck with .. I have to convert a string (from some external source) using C# code .. the string can have these expected format of DateTime ..
02/31/2009 01:59:59 24 hours format
02/31/2009 01:59:59 AM 12 hours format
2/31/2009 1:59:59
2/31/2009 1:59:59 AM
02/01/2009 01:59:59 AM
2/1/2009 1:59:59
and so on .......
I tried using DateTime(Convert.ToInt32(string_date.Substring(6,4)),Int,Int,Int,Int,Int,Int) ie, By extracting the values of month, Day etcBut it doesn't work .. because I can't extract the values with substring perfectly .. as the length of string is Varying I also have tried to extract the values referring the occurance of "/", "space" and ":" but it becomes bottle neck to derive with (non-)Occurrence of AM/PM Only the length of Day, Month and Hours can vary ..
You can use the DateTime.ParseExact overload that takes a list of formats:
private static string[] formats = new string[]
{
"MM/dd/yyyy HH:mm:ss tt",
"MM/dd/yyyy HH:mm:ss",
"M/dd/yyyy H:mm:ss tt",
"M/dd/yyyy H:mm:ss"
};
private static DateTime ParseDate(string input)
{
return DateTime.ParseExact(input, formats, CultureInfo.InvariantCulture, DateTimeStyles.None);
}
This will throw a FormatException if the passed string does not match any of the given formats. Notice that the formats expecting AM/PM should appear before identical formats without AM/PM ("MM/dd/yyyy HH:mm:ss tt" comes before "MM/dd/yyyy HH:mm:ss").
Update
As Henk points out in the comments, the same functionality is available when using TryParseExact which removes exception situation. Also, paired with nullable types this can be made a bit cleaner:
private static DateTime? ParseDate(string input)
{
DateTime result;
if (DateTime.TryParseExact(input, formats, CultureInfo.InvariantCulture, DateTimeStyles.None, out result))
{
return result;
}
return null;
}
Now it will simply return a null reference if it fails to parse the input.
Take a look at the TryParseExact method. Here's an example with the first case:
DateTime date;
// I changed 02/31/2009 to 01/31/2009 because the first is not a valid date
if (DateTime.TryParseExact("01/31/2009 01:59:59", "MM/dd/yyyy HH:mm:ss", null, DateTimeStyles.None, out date))
{
// string successfully parsed => do something with the date
}
You could then keep a list of different formats and try to parse the string with all of them until you succeed.
Here are all the possible formats ..
MM/dd/yyyy 08/22/2006
dddd, dd MMMM yyyy Tuesday, 22
August 2006
dddd, dd MMMM yyyy HH:mm Tuesday,
22 August 2006 06:30
dddd, dd MMMM yyyy hh:mm tt
Tuesday, 22 August 2006 06:30 AM
dddd, dd MMMM yyyy H:mm Tuesday, 22
August 2006 6:30
dddd, dd MMMM yyyy h:mm tt Tuesday,
22 August 2006 6:30 AM
dddd, dd MMMM yyyy HH:mm:ss
Tuesday, 22 August 2006 06:30:07
MM/dd/yyyy HH:mm 08/22/2006 06:30
MM/dd/yyyy hh:mm tt 08/22/2006
06:30 AM
MM/dd/yyyy H:mm 08/22/2006 6:30
MM/dd/yyyy HH:mm:ss 08/22/2006
06:30:07
MMMM dd August 22
yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK
2006-08-22T06:30:07.7199222-04:00
ddd, dd MMM yyyy HH':'mm':'ss 'GMT'
Tue, 22 Aug 2006 06:30:07 GMT
yyyy'-'MM'-'dd'T'HH':'mm':'ss
2006-08-22T06:30:07
HH:mm 06:30
hh:mm tt 06:30 AM
H:mm 6:30
h:mm tt 6:30 AM
HH:mm:ss 06:30:07
yyyy'-'MM'-'dd HH':'mm':'ss'Z'
2006-08-22 06:30:07Z
dddd, dd MMMM yyyy HH:mm:ss
Tuesday, 22 August 2006 06:30:07
yyyy MMMM 2006 August
DateTime dt1 = DateTime.ParseExact("2007/01/01 04:23:12", "yyyy/MM/dd hh:mm:ss",
System.Globalization.CultureInfo.CurrentCulture);
DateTime dt = Convert.ToDateTime("2007/01/01 04:23:12", System.Globalization.CultureInfo.CurrentCulture);
System.Globalization.CultureInfo.CurrentCulture
format param

Categories

Resources