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
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
I'm passing a date back to my MVC controller via AJAX using:
date.GetDate().toLocaleDateString();
This will produce a value of "4/5/2014"... When I try and convert this inside my controller using:
DateTime myDate = DateTime.ParseExact(request.Params["myDate"], "dd/MM/yyyy", CultureInfo.InvariantCulture);
I get "String was not recognized as a valid DateTime." Which makes sense because the format of the string isn't correct... When I hardcode the string to: "04/05/2014" it will fix my issue.
Is there anyway to fix the format coming from javascript without have to rip the string apart into day, month, year and reassembling it in the proper format?
Any advice would be much appreciated!
Thank you
Additional info:
string myRequestDate = request.Params["myDate"];
string myViewBagDate = ViewBag.MyDate;
//This line passes
DateTime date1 = DateTime.ParseExact(myViewBagDate, "d/M/yyyy", CultureInfo.InvariantCulture);
//This line fails...
DateTime date5 = DateTime.ParseExact(myRequestDate, "d/M/yyyy", CultureInfo.InvariantCulture);
When I add a watch on both of the string variables the values are identical in every way I can see but for some reason the second line fails...
So when I look at the myRequestDate as a char array I see there is a bunch of stuff in there that doesn't look like a date at all...
Character 8206 (U+200E) is the Unicode LEFT-TO-RIGHT MARK (which is invisible).
Try to figure out where it's coming from and remove it at the source.
As a workaround, you can strip out those characters before you parse the date:
myRequestDate = myRequestDate.Replace("\u200E", "");
DateTime date5 = DateTime.ParseExact(myRequestDate, "d/M/yyyy", CultureInfo.InvariantCulture);
Just change the format string you use to match what you get from the JavaScript date.
DateTime.ParseExact(request.Params["myDate"], "d/M/yyyy", CultureInfo.InvariantCulture);
This code:
var d=new Date();
console.log(d.toString());
In windows 8.1 Chrome does this output
"Wed Jun 04 2014 20:38:23 GMT+0200 (Hora de verano romance)"
But in Cromium in Kubuntu
"Wed Jun 04 2014 20:38:23 GMT+0200 (CEST)"
Very similar, but other browser return diferent kind of formats, i use this function, this work fine with various kinds of formats.
public static DateTime ParseDate(string value)
{
if (value == null)
{
throw new ArgumentNullException("value");
}
string[] formats = {"ddd MMM dd yyyy hh:mm:ss 'UTC'zzz",
"ddd MMM d yyyy hh:mm:ss 'UTC'zzz",
"ddd MMM d hh:mm:ss 'UTC'zzz yyyy",
"ddd MMM dd hh:mm:ss 'UTC'zzz yyyy",
"ddd MMM dd yyyy hh:mm:ss 'GMT'zzz",
"ddd MMM d yyyy hh:mm:ss 'GMT'zzz",
"ddd MMM d hh:mm:ss 'GMT'zzz yyyy",
"ddd MMM dd hh:mm:ss 'GMT'zzz yyyy",
"dd-MM-yyyy",
"yyyy-MM-dd'T'hh:mm:ss"
};
DateTime fecha;
//Try the default
if (!DateTime.TryParse(value, out fecha))
{
if (!DateTime.TryParseExact(value, formats,
new CultureInfo("en-US"),
DateTimeStyles.None, out fecha))
{
if (value.Length > 24)
{
return ParseDate(value.Substring(0, 24));
}
else
{
throw new FormatException();
}
}
}
return fecha;
}
Use the following code at the client side:
(date.GetDate()).toDateString();
At your controller side:
var date = Convert.ToDateTime(Request.Params["myDate"]);
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.
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);
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