Let's say I have the following dates/times in some in-house almost-ISO-like format:
"2011-11-07T11:17"
"--T11:17"
(11:17 am, no date, only time)"-11-07"
(november the 7th, no year, no time)
The separators are mandatory, and enable me to know is a data is present, or not. The data would be set into a structure like:
struct MyDate
{
int? Year ;
int? Month ;
int? Day ;
int? Hour ;
int? Minute ;
}
The "easiest" way would be to loop character by character, and extract the data if present.
But I have the nagging impression that there must be some kind of API to extract, for example, an integer and return the index of the first non-integer character (similar to C's strtol
).
Is there a strtol
-like function in C#, or something more high-level to extract typed data, instead of parsing the string character by character?
You can use
DateTime.ParseExact
. Check documentation here