DateTime.TryParse fails in Windows 7, when we change the regional settings to Italian.I even tried TryParseExact but with no luck. Does anybody have any idea on this or came across this type of scenario?
Code is some thing like this:
string[] formats = {"M/d/yyyy h:mm:ss tt", "M/d/yyyy h:mm tt", "MM/dd/yyyy hh:mm:ss", "M/d/yyyy h:mm:ss", "M/d/yyyy hh:mm tt", "M/d/yyyy hh tt", "M/d/yyyy h:mm", "M/d/yyyy h:mm", "MM/dd/yyyy hh:mm", "M/dd/yyyy hh:mm", "dd/MM/yyyy HH:mm"};
if (DateTime.TryParseExact(cb.Text, formats, CultureInfo.InVariantCulture, DateTimeStyles.AllowLeadingWhite, out date_and_time))
but it returns false.
or
Even tried:
if (DateTime.TryParse(cb.Text, CultureInfo.InvariantCulture, DateTimeStyles.None,out date_and_time) == true)`
cb.Text is a String which contains the DateTime in string representation.
Have you tried calling it with a neutral CultureInfo?
Like this
DateTime parsed;
if(DateTime.TryParse("2010-03-09", CultureInfo.InvariantCulture, DateTimeStyles.None, out parsed))
Console.WriteLine(parsed)
Or for TryParseExact
DateTime.TryParseExact("2010-03-09", "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out parsed)
In Italian the time separator token is resolved to . rather than :
Try escaping the time separator token in single quotes for example:
"M/d/yyyy h':'mm':'ss tt"
Try setting Thread Culture to Italian Culture using CreateSpecificCulture method.
See list of cultures here.
Related
I cannot understand why this is throwing "String was not recognized as a valid DateTime"
string[] formats = {"M/d/yyyy h:mm:ss tt", "M/d/yyyy h:mm tt",
"MM/dd/yyyy hh:mm:ss", "M/d/yyyy h:mm:ss",
"M/d/yyyy hh:mm tt", "M/d/yyyy hh tt",
"M/d/yyyy h:mm", "M/d/yyyy h:mm", "MM/dd/yyyy hh:mm",
"MM/dd/yyyy hh:mm", "M/dd/yyyy hh:mm",
"MM/d/yyyy HH:mm:ss.ffffff" };
var delimit = line.Split(',');
try
{
id = delimit[0];
DateTime.TryParseExact(delimit[1].Trim(),formats,new CultureInfo("en-US"),DateTimeStyles.None, out openDate);
delimit[5] = delimit[5].Replace("\"","");
closedDate = DateTime.ParseExact(delimit[5].Trim(),formats,new CultureInfo("en-US"),DateTimeStyles.None);
DateTime.TryParseExact(delimit[5].Trim(),formats,new CultureInfo("en-US"),DateTimeStyles.None, out closedDate);
severity = delimit[7].Split('-').Last().Trim();
state = delimit[6].Trim();
}
catch(Exception e)
{
Console.WriteLine(line);
}
The entry it throws on is :
The formats array should include the 24 hour time format.
Either dd/MM/yyyy HH:mm OR MM/dd/yyyy HH:mm OR M/d/yyyy HH:mm OR d/M/yyyy H:m format can be used.
Please note that only you know whether your format is dd-MM-yyyy or MM-dd-yyyy as for both date and month places you have 12.
You can check the sample inputs to your code and depending on those you should choose the format carefully.
Hope this helps.
I have some code that I want to test for a valid time and for some reason it works for dates with PM's but not AM.
So I'm taking in formats that correspond to this format:
6/1/2018 12:00:00 PM
However for some reason or another this is working great when it's PM but when I replace it with AM it breaks. The format in acceptableformats I'm accepting it to hit is:
M/d/yyyy HH:mm:ss tt
The below fails saying that it's false:
DateTimeOffset dateTimeResult;
var acceptableFormats = new string[] {
"yyyy-MM-dd'T'HH:mm:ss.FFFK",
"M/d/yyyy HH:mm:ss",
"MM/dd/yyyy HH:mm:ss",
"M/d/yyyy HH:mm:ss tt",
"MM/dd/yyyy HH:mm:ss tt",
"yyyy-MM-dd' 'HH:mm:ss.FFFK",
"yyyy-MM-dd'T'HH:mm:ssK",
"yyyy-MM-dd' 'HH:mm:ssK",
"yyyy-MM-dd'T'HH:mm:ss",
"yyyy-MM-dd' 'HH:mm:ss",
"yyyy-MM-dd'T'HH:mm",
"yyyy-MM-dd' 'HH:mm",
"yyyy-MM-dd'T'HH",
"yyyy-MM-dd' 'HH",
"yyyy-MM-dd",
"yyyy-MM-dd",
"yyyyMMdd",
"MM/dd/yyyy",
"M/d/yyyy",
"yyyy-MM",
"yyyy" };
DateTimeOffset dateTimeResult;
var timeOffset = DateTimeOffset.TryParseExact("6/1/2018 12:00:00 AM", acceptableFormats, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTimeResult);
This works says it's true:
DateTimeOffset dateTimeResult;
var acceptableFormats = new string[] {
"yyyy-MM-dd'T'HH:mm:ss.FFFK",
"M/d/yyyy HH:mm:ss",
"MM/dd/yyyy HH:mm:ss",
"M/d/yyyy HH:mm:ss tt",
"MM/dd/yyyy HH:mm:ss tt",
"yyyy-MM-dd' 'HH:mm:ss.FFFK",
"yyyy-MM-dd'T'HH:mm:ssK",
"yyyy-MM-dd' 'HH:mm:ssK",
"yyyy-MM-dd'T'HH:mm:ss",
"yyyy-MM-dd' 'HH:mm:ss",
"yyyy-MM-dd'T'HH:mm",
"yyyy-MM-dd' 'HH:mm",
"yyyy-MM-dd'T'HH",
"yyyy-MM-dd' 'HH",
"yyyy-MM-dd",
"yyyy-MM-dd",
"yyyyMMdd",
"MM/dd/yyyy",
"M/d/yyyy",
"yyyy-MM",
"yyyy" };
DateTimeOffset dateTimeResult;
var timeOffset = DateTimeOffset.TryParseExact("6/1/2018 12:00:00 PM", acceptableFormats, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTimeResult);
The reason you have this problem is because your format is asking for a 24-hour hour component. 12, in 24-hour time, ALWAYS means "noon". You can't have "noon AM" because AM is only for times between midnight and noon (exclusive).
This line throws an exception because of an invalid input format:
DateTimeOffset.ParseExact("6/1/2018 12:00:00 AM", "M/d/yyyy HH:mm:ss tt",
CultureInfo.InvariantCulture)
If I change HH to hh, then it works:
DateTimeOffset.ParseExact("6/1/2018 12:00:00 AM", "M/d/yyyy hh:mm:ss tt",
CultureInfo.InvariantCulture)
// returns a valid DateTimeOffset object, which in
// my culture is displayed as '6/1/2018 12:00:00 AM -04:00'
// (I happen to be in -4 UTC)
I was using DateTime.ParseExact to convert my date string value to Date object.
Here is the code that I use to convert my date string to DateTime object.
DateTime.ParseExact(“ my date string value from database ”, "dd/MM/yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture) ;
My database returns a string value like this “25/9/2016 00:00:00”.
But when I was debug my code it continuously gave me that string is not valid string to convert to DateTime object.
Finality I have to change my laptop date time format to “d/M/yyyy H:mm:ss” to make my database string value to valid string value.
So how can I guarantee that my code work in server. What is the solution for this kind of situations ?? Do I have to check server date time format and change to format string according to it before build my code.??
ParseExact with the format "dd/MM/yyyy HH:mm:ss" will fail when you give it the string “25/9/2016 00:00:00” because the month is just a single digit.
Changing the format to "dd/M/yyyy HH:mm:ss" is the correct solution.
However, you shouldn't be storing dates & times in your database as strings as you will keep encountering this sort of problem. One of the main causes will be different settings when the date to string conversion happens. The main ones will be day-month-year vs month-day-year and single vs double digits for the days and months. Also if you're converting to local time rather than UTC then events will appear out of order in the database if you have users in different time zones.
If you really have to have strings then you can use the overload of ParseExact that takes an array of allowable formats so you can cope with multiple formats as in the example from that page:
string[] formats= {"M/d/yyyy h:mm:ss tt", "M/d/yyyy h:mm tt",
"MM/dd/yyyy hh:mm:ss", "M/d/yyyy h:mm:ss",
"M/d/yyyy hh:mm tt", "M/d/yyyy hh tt",
"M/d/yyyy h:mm", "M/d/yyyy h:mm",
"MM/dd/yyyy hh:mm", "M/dd/yyyy hh:mm",
"MM/d/yyyy HH:mm:ss.ffffff" };
string[] dateStrings = {"5/1/2009 6:32 PM", "05/01/2009 6:32:05 PM",
"5/1/2009 6:32:00", "05/01/2009 06:32",
"05/01/2009 06:32:00 PM", "05/01/2009 06:32:00",
"08/28/2015 16:17:39.125", "08/28/2015 16:17:39.125000" };
DateTime dateValue;
foreach (string dateString in dateStrings)
{
try {
dateValue = DateTime.ParseExact(dateString, formats,
new CultureInfo("en-US"),
DateTimeStyles.None);
Console.WriteLine("Converted '{0}' to {1}.", dateString, dateValue);
}
catch (FormatException) {
Console.WriteLine("Unable to convert '{0}' to a date.", dateString);
}
}
I have string of Date which can be in any format of date but I wanted to convert it to dd-MM-yyyy format.
I have tried every Convert.ToDatetime option which converts only to the System format. I want it to convert dd-MM-yyyy format.
Please reply. Thanks in Advance.
Try this
DateTime dateTime = new DateTime();
dateTime = Convert.ToDateTime(DateTime.ParseExact("YouDateString", "dd-MM-yyyy", CultureInfo.InvariantCulture));
This will return DateTime with your Format
Despite there being many answers and this being a duplicate answer, here are some possible solutions:
string formatted = date.ToString("dd-MM-yyyy");
or
string formatted = date.ToString("dd MM yyyy");
This link might help you with more formats and options.
DateTime.ParseExact has an overloaded method to accepts multiple formats, include (all)possible formats you receive and parse the string. Once you get the valid DateTime you could apply desired format in converting to string.
// ex...
string dateString = ...; // your date.
string[] formats= {"M/d/yyyy h:mm:ss tt", "M/d/yyyy h:mm tt",
"MM/dd/yyyy hh:mm:ss", "M/d/yyyy h:mm:ss",
"M/d/yyyy hh:mm tt", "M/d/yyyy hh tt",
"M/d/yyyy h:mm", "M/d/yyyy h:mm",
"MM/dd/yyyy hh:mm", "M/dd/yyyy hh:mm",
"MM/d/yyyy HH:mm:ss.ffffff" };
var date = DateTime.ParseExact(dateString, formats, new CultureInfo("en-US"), DateTimeStyles.None);
//convert to desired format.
var strDate = date.ToString("dd-MM-yyyy");
Here we go:
DateTime time = DateTime.Now;
Console.WriteLine(time.Day + "/" + time.Month + "/" + time.Year);
//return: 22/05/2016
How can i parse a string like this: "2/22/2015 9:54:02 AM" to a DateTime instance?
i am currently using the DateTime.ParseExact method but without the AM/PM
i.e:
DateTime.ParseExact("2/22/2015 9:54:02", "M/dd/yyyy HH:mm:ss")
I would like to be able to parse the AM/PM signs as well.
You should change the hour format (H) to lowercase like this:
DateTime.ParseExact("2/22/2015 9:54:02 AM", "M/d/yyyy h:mm:ss tt", CultureInfo.InvariantCulture);
Uppercase "H" indicates a 24-hour time and lowercase "h" indicates 12-hour time and will respect the AM/PM in the candidate string.
You can use the tt specifier:
DateTime.ParseExact(
"2/22/2015 9:54:02 PM",
"M/dd/yyyy h:mm:ss tt",
CultureInfo.InvariantCulture
)
However be warned this can be locale specific. Also HH refers to the 24 hour clock, with AM/PM you generally use the 12 hour clock, so you'd want to use hh or just h for that.
Try This,
DateTime.ParseExact("2/22/2015 9:54:02 PM", "M/dd/yyyy h:mm:ss tt", CultureInfo.InvariantCulture);
try this:
if (date.Contains("AM") || date.Contains("PM"))
return DateTime.ParseExact(date, "dd.MM.yyyy h:mm:ss tt", CultureInfo.InvariantCulture);
return DateTime.ParseExact(date, "dd.MM.yyyy HH:mm:ss", CultureInfo.InvariantCulture);