How to convert a string containing AM/PM to DateTime? - c#

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);

Related

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 conversion increasing month by 1

Trying to use ParseExact to convert a string to datetime but the resulted datetime seems to be increasing the month by 1. What am I missing
DateTime.ParseExact("7/22/2015 8:08:01 PM", "m/d/yyyy h:M:s tt", CultureInfo.InvariantCulture)
Result: 22-08-2015 20:07:01
You mixed up m and M for minutes and months. So it's just coincidence it looks like the month is increased by 1.
The correct code would be:
DateTime.ParseExact("7/22/2015 8:08:01 PM", "M/d/yyyy h:m:s tt", CultureInfo.InvariantCulture)
Lol, change it to :
var d = DateTime.ParseExact("7/22/2015 8:08:01 PM", "M/d/yyyy h:m:s tt", CultureInfo.InvariantCulture);
m is minute, M is month.
The code is not increasing the month by one but picking out the 08 minute part of the input. You want
"M/d/yyyy h:m:s tt"

How to converting in to time?

I am trying to convert to time from a string
My string are like this "11:45 AM" or "03:19 PM" and i am using
dateTime = DateTime.ParseExact("11:45 AM", "H:mm tt",
System.Globalization.CultureInfo.InvariantCulture);
Then it is getting converted but when i am passing
DateTime.ParseExact("3:19 PM", "H:mm tt",
System.Globalization.CultureInfo.InvariantCulture).ToString();
Getting error as
String was not recognized as a valid DateTime.
I cant understand why it is happening any one have idea then please help me
I would use h instead of H. H is for the 24hr fromat, h for the 12hr format.
DateTime.ParseExact("9:45 PM", "h:mm tt", System.Globalization.CultureInfo.InvariantCulture)
See the full list of format options here.
As you want to parse the 12 hr format and convert it to the 24 hr format then you can just use this
string dt = DateTime.ParseExact("3:19 PM", "h:mm tt",CultureInfo.InvariantCulture).ToString("HH:mm");;
Unfortunately, none of the answers are completely correct.
Ante meridiem and post meridiem belong to the 12-hour clock format. That's why you should never use 24-hour clock format specifiers if your string contains one of them.
That's why you need to use h or hh specifiers, not H or HH. Since your hour part can be with leading zeros, using hh specifier is the best option for both of your string types.
Using the hh:mm tt format will parse your strings successfully.
string s = "03:19 PM";
DateTime dt;
if(DateTime.TryParseExact(s, "hh:mm tt", CultureInfo.InvariantCulture,
DateTimeStyles.None, out dt))
{
dt.Dump(); // 29.05.2015 15:19:00
}
and
string s = "11:45 AM";
DateTime dt;
if(DateTime.TryParseExact(s, "hh:mm tt", CultureInfo.InvariantCulture,
DateTimeStyles.None, out dt))
{
dt.Dump(); // 29.05.2015 11:45:00
}
Also, you mentioned the 3:19 PM string in your code example. Since the hour part is single digit, you need to use the h:mm tt format in that case.
Invariant culture requires two-digit hours.

Can't remove hidden characters from string

I'm trying to remove hidden characters from a string that represents a date time. I'm using .Net Fiddle and you can see the line that tries to ParseExact fails.
Here is a snippet. Please refer to the fiddle link for working code.
var dateTime = "2015-04-14 07:30:00 PM"; //<= this throws an error from some hidden char
dateTime = Regex.Replace(dateTime, #"[^\w:\s-]", "");
Console.WriteLine(dateTime);
DateTime dateWithTime = DateTime.ParseExact(dateTime, "yyyy-MM-dd HH:mm:ss tt",
CultureInfo.InvariantCulture);
Console.WriteLine("OK");
The HH in the format string refers to the 24-hour clock hours, which doesn't work when using AM/PM in the format string for PM times.
Change HH to hh.
It's not an invisible character. Your use of HH conflicts with your use of tt. HH is 24 hour time, but you are using tt to interpret PM (12 hour time). Change it to hh and it works.
var dateTime = "2015-04-14 07:30:00 PM";
//dateTime = Regex.Replace(dateTime, #"[^\w:\s-]", ""); <= not needed
Console.WriteLine(dateTime);
DateTime dateWithTime = DateTime.ParseExact(dateTime, "yyyy-MM-dd hh:mm:ss tt",
CultureInfo.InvariantCulture);
Console.WriteLine("OK");
You need to change HH to hh.
yyyy-MM-dd hh: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