Having issues with AM/PM with DateTimeOffset.TryParseExact - c#

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)

Related

Why DateTime TryParse is returning date as {01/01/0001 00:00:00}? [duplicate]

This question already has answers here:
How do I format a DateTime in a different format?
(2 answers)
Closed 3 years ago.
I am doing this
string[] formats = { "yyyy-MM-dd" };
DateTime outDate;
DateTime.TryParseExact(DateTime.Now.ToString(),
formats,
new CultureInfo("en-US"),
DateTimeStyles.None,
out outDate);
interfaceoperation.LogDate = outDate;
interfaceoperation.LogTime = outDate;
LogDate and LogTime are of type DateTime.
But it returns outdate value as {01/01/0001 00:00:00}.
Why?
I think the problem is in the format of the dates, we will have to add the correct format of your date.
DateTime.TryParseExact(DateTime.Now.ToString(), DateTime.Now.GetDateTimeFormats(),
new CultureInfo("en-US"),
DateTimeStyles.None, out outDate);
or we will have to add the correct format of your date.
Or you can retrieve it directly from the wafer you use 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"};
Well, DateTime.TryParseExact returns false and DateTime.MinValue (which is 01/01/0001 00:00:00) whenever it fails to parse the given string.
https://learn.microsoft.com/en-us/dotnet/api/system.datetime.tryparseexact?view=netframework-4.8
When this method returns, contains the DateTime value equivalent to
the date and time contained in s, if the conversion succeeded, or
MinValue if the conversion failed. The conversion fails if either the
s or format parameter is null, is an empty string, or does not contain
a date and time that correspond to the pattern specified in format.
This parameter is passed uninitialized.
It seems, you want more formats to be mentioned:
// Test value
// Something like "10/08/2019 2:47:58 PM"
string value = DateTime.Now.ToString(CultureInfo.GetCultureInfo("en-US"));
...
string[] formats = {
"R", // RFC1123 e.g. 2019-10-08T14:39:47
"yyyy-M-d",
"yyyy-M-d H:m:s",
"M/d/yyyy", // en-US special: date only
"M/d/yyyy H:m:s", // en-US special: date and 24 hour time
"M/d/yyyy h:m:s tt", // en-US special: date and 12 hour time
};
DateTime outDate;
if (DateTime.TryParseExact(value,
formats,
CultureInfo.GetCultureInfo("en-US"),
DateTimeStyles.None, out outDate)) {
// Parsing succeeded
interfaceoperation.LogDate = outDate;
interfaceoperation.LogTime = outDate;
}
else
{
// Parsing failed
}

DateTime.Parse throwing string not recognized, I cannot see why

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.

DateTime.ParseExact is not working, even though the format is correct

DateTime.ParseExact is giving an excpetion 'String was not recognized as a valid DateTime.' for the below code.
DateTime colValue = DateTime.ParseExact("11-Oct-18 11:15:13 AM", "dd-MM-yyyy hh:mm:ss",
CultureInfo.InvariantCulture);
Why this is not working?
There are a few things to fix in your mask:
Long Month is => MMM
You are using a short year => yy
You need to indicate the AM/PM => tt
Hour-Minute-Second should support one digit => h:m:s
DateTime colValue = DateTime.ParseExact(
"11-Oct-18 11:15:13 AM",
"dd-MMM-yy h:m:s tt",
CultureInfo.InvariantCulture);
This works because we replace hh with h, mm with m and ss with s, we also added tt to catch AM or PM:
DateTime colValue = DateTime.ParseExact("11-Oct-18 11:15:13 AM", "dd-MMM-yy h:m:s tt",
System.Globalization.CultureInfo.InvariantCulture);
Thanks to #Jerin Sebastian

DateTime.ParseExact returns wrong date for the 'dd/MM/yyyy h:mm tt' format

I need to convert datetime from MM/dd/yyyy h:mm:ss tt format to the dd/MM/yyyy h:mm:ss tt
My code
var date = ((DateTime)model.WorkshopDate).ToString("dd/MM/yyyy h:mm:ss tt");
var resultDate = DateTime.ParseExact(date, "dd/MM/yyyy h:mm:ss tt", CultureInfo.InvariantCulture);
In result date = 25/12/2017 12:00:00 AM
But resultDate = 12/25/2017 12:00:00 AM .
How can i parse it right?
You are looking at the object in the debugger which is a datetime object and not a display version of your date.
Example:
var theDate = DateTime.UtcNow;
var date = theDate.ToString( "dd/MM/yyyy h:mm:ss tt" );
var resultDate = DateTime.ParseExact( date, "dd/MM/yyyy h:mm:ss tt", CultureInfo.InvariantCulture );
Console.WriteLine(date == resultDate.ToString("dd/MM/yyyy h:mm:ss tt"));//Returns true
As you see they are the same dates so just .ToString() it to whatever format you need when displaying.
You don't need to convert it to a String and back to a DateTime. The resulting DateTime object should have the same Date. You just need to convert it when you want to display it somewhere:
((DateTime)model.WorkshopDate).ToString("dd/MM/yyyy h:mm:ss tt");
here
var date = ((DateTime)model.WorkshopDate).ToString("dd/MM/yyyy h:mm:ss tt");
you are implicitly asking to display the date in "dd/MM/yyyy h:mm:ss tt"
var resultDate = DateTime.ParseExact(date, "dd/MM/yyyy h:mm:ss tt", CultureInfo.InvariantCulture);
here system default format is using to display the date.
if you want to display both as same
Console.WriteLine(date);
Console.WriteLine(resultDate.ToString("dd/MM/yyyy h:mm:ss tt"));

DateTime.TryParse() fails in Windows 7

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.

Categories