I thought this would be easy: I want to parse the following string format to DateTime
:
"4/25/18 3:11 PM"
and it's a lot more difficult than I expected.
DateTime.Parse
just returns an exception Input string not in the correct format
DateTime.TryParseExact
is the closest I have come, and it can parse this exact string, but it does not account for when day, month, hour (etc) goes over (or below, depending on the mask) 9 since the mask has to match exactly or it will fail.
string input = "4/25/18 3:11 PM";
string input2 = "1/1/18 10:10 AM";
DateTime theDate;
DateTime.TryParseExact(input, "M/dd/yy h:mm tt", CultureInfo.InvariantCulture, DateTimeStyles.None, out theDate);
My next idea was to split out the 4
, 25
, 18
and add a 0 if they are < 10, and a 20 concatenated before the 18 but this seems overkill. It also leaves me with the time and making conditions based on if the tt
is AM or PM.
EDIT: Based on some comments....
I have no control over the input string.
Convert.ToDateTime("25/4/18 3:11 PM")
throws an exception {"String was not recognized as a valid DateTime."}
When I specified the input date as 4/25/18, the input format is clearly M/dd/yy. Unfortunately this can mean the input date can be MM/d/yy etc. We can assume it will always be Month / Day / Year..
TLDR: How can I parse input
and input2
to a DateTime cleanly?
Thanks
You can change your code to be like this, and then it should accept both formats with single and double digits:
DateTime.TryParseExact(
input, "M/d/yy h:mm tt",CultureInfo.InvariantCulture, DateTimeStyles.None,
out var theDate);
The single character M
, d
,and h
components in the format string allow for single or double digits.
If the code is executed in US Locale and the date string is known or expected to be a valid US date format:
Convert.ToDateTime("4/25/18 3:11 PM")
For other locales (assuming the input string is valid in the current culture) try this overload:
Convert.ToDateTime("25/4/18 3:11 PM", System.Globalization.CultureInfo.CurrentCulture)
However, based on your revision and comments if I understand correctly, you expect the date will always be in M/D/Y (US) format:
I have no control over the input string.
Convert.ToDateTime("25/4/18 3:11 PM")
throws an exception{"String was not recognized as a valid DateTime."}
When I specified the input date as 4/25/18, the input format is clearly M/dd/yy. Unfortunately this can mean the input date can be MM/d/yy etc. We can assume it will always be Month / Day / Year.
Sure. In that case you're attempting to execute against an invalid US date, on a US culture. But if you can assume the date format provided in string will always be month/day/year, then you should be able to do:
Convert.ToDateTime("4/25/18 3:11 PM", new System.Globalization.CultureInfo("en-US"))
This seems to work whether month/day are provided in single or double-digit, and should return valid DateTime
object based on assumed en-US date string.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.