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"
Related
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
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.
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
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);
My Date Format : 12/30/2014 12:00:00 AM (MM/dd/yyyy) and I have a string rep as
String DateOfIssue = "12/30/2014 12:00:00 AM";
DateTime DOI = DateTime.ParseExact((DateOfIssue).Trim(), "MM/dd/yyyy HH:mm:ss tt", CultureInfo.GetCultureInfo("en-GB"));
but it is throwing exception. I have seen a lot of posts on SO but none is working. What am I doing wrong ? Please help and point my error. I have tried en-US also but that is also not working.
when I remove tt every thing works fine.
HH - is for 24 hours format, use hh instead:
DateTime DOI = DateTime.ParseExact((DateOfIssue).Trim(), "MM/dd/yyyy hh:mm:ss tt", CultureInfo.GetCultureInfo("en-GB"));