Parse Datetime string - c#

I'm trying to convert the following string to datetime. I've searched high and low and can't find the exact formats string and I don't want to resort to parsing it manually.
var dateString = "20110828T134108+0100";
All my attempts fail with FormatException.

Have you tried this?
var date = DateTime.ParseExact( dateString
,"yyyyMMdd\THHmmsszzz"
,CultureInfo.InvariantCulture
);
From MSDN:
If format is a custom format pattern that does not include date or
time separators (such as "yyyyMMdd HHmm"), use the invariant culture
for the provider parameter and the widest form of each custom format
specifier. For example, if you want to specify hours in the format
pattern, specify the wider form, "HH", instead of the narrower form,
"H".

The documentation on the format string is here: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
That should get you started. :)

try this format: "yyyyMMdd'T'HHmmss"

Have you tried this:
DateTime.ParseExact("20110828T134108+0100", "yyyyMMdd'T'HHmmsszzzz", CultureInfo.InvariantCulture);

Related

To pass specific Date format

How we can pass specific Date format like "MM/dd/YYYY" in place of System.DateTime in below code.
new ObjectParameter("FromDate", typeof(System.DateTime));
I'm not sure but i guess you are looking for a way to convert the DateTime to string in this format. Then you can use DateTime.ToString with an appropriate format string:
string result = DateTime.Now.ToString("MM/dd/yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo);
I'm using InvariantInfo to force the / as separator. Otherwise it would be replaced with your localized date separator. See: The "/" Custom Format Specifier.
Another way to force / as date separator is to escape the / format specifier with ':
string result = DateTime.Now.ToString("MM'/'dd'/'yyyy");
You can't.
A DateTime does not have any implicit format. It just have date and time values. Format concept only applies when you get it's textual (string) representation.
There is only two constructor of that ObjectParameter which one is expect Object and the other one expect Type as a second parameter.
You can specify how you want the DateTime formatted when using .ToString(), by entering a parameter in the ToString() method, like so: somedatetime.ToString("d");
You can read more about the formatting here: DateTime.ToString Method at MSDN

DateTime Format provider for filepath

I have a file-path that's been created from DateTime stamp:
"C:\\Logs\\Tests\\2015\\Mar\\24\\13_32_09\"
Now I am trying to convert my file-path back to DateTime object.
With Regex I can easily remove "C:\\Logs\\Tests\", but now I am assume I need to provide implementation of IFormtProvider to convert 2015\\Mar\\24\\13_32_09\ into a DateTime object, but I haven't come along any similar example of how that's usually done.
Any example, may not be particular solution to my answer, would be helpful.
Thanks
You can use DateTime.ParseExact like:
DateTime dt = DateTime.ParseExact("2015\\Mar\\24\\13_32_09\\",
#"yyyy\\MMM\\dd\\HH_mm_ss\\",
CultureInfo.InvariantCulture);
No, you don't need to create an IFormatProvider at all. The invariant culture is fine for this (assuming the month name is always in English). You can just use DateTime.ParseExact, passing in the appropriate custom format string (quoting the literal characters, either with apostrophes around them or backslashes before them):
var dateTime = DateTime.ParseExact(
text,
#"yyyy'\'MMM'\'dd'\'HH'_'mm'_'ss'\'",
CultureInfo.InvariantCulture);
Note that this assumes the path really does use backslashes... it won't work on Unix as-is. (You might want to canonicalize the directory separators first.)

Parse DateTime in C#

I am trying to parse a TimeString that looks like:
11/Apr/2014:00:00:12 +0200
my code looks like
DateTime.ParseExact("11/Apr/2014:00:00:12 +0200", "dd/MMM/yyyy:HH:mm:ss zzz", null)
I looked at the MSDN and it looks good for me but I have no clue why I always get a FormatException.
You should add the InvariantCulture as a format provider.
var d = DateTime.ParseExact("11/Apr/2014:00:00:12 +0200", "dd/MMM/yyyy:HH:mm:ss zzz", CultureInfo.InvariantCulture);
Your format string is considering that the / and : characters are specific format separators that will resolve to the ones defined in your current culture, just as HH would signify "hours" in your format. Please refer to this page to see that the time separator and date separator are predefined and will be replaced by the culture specific values.
It is possible to escape the special characters but I think that in the long run your code will be much safer with the InvariantCulture

String Parsing a Time

I need to obtain a time in the format of "07:30 am" (not case-sensitive). I'm reading a file which has this in the format "07:30am". Eventually I will be constructing a DateTime from this, so I just need to get this back with a space before the am/pm part.
I can detect the occurrence of the a or p using this:
if(startString.IndexOfAny("ap".ToCharArray()) != -1)
{
}
What's the best ways to do this? I'm guessing I will end up with two strings that can be concatenated with a space? Can Split be used with the above snippet to achieve this?
UPDATE:
I need to end up with a space in the DateTime between the minutes and the AM/PM and I do not want to use regular expressions. So far, nothing I've tried here gives me that...
The actual input I have to handle is in this format:
RecDate: "04/30/2012"
RecTime: "05:30am"
I need to create a new DateTime object from these with a space before the am/pm part.
You have two easy choices:
Use RegEx to fix the formatting:
string newTime = Regex.Replace(startString, #"(?<=[01]\d:[0-5]\d)(?=[ap]m)", " ");
Use DateTime.ParseExact to just import the time as-is:
DateTime newValue = DateTime.ParseExact(
startString,
"hh:mmtt",
CultureInfo.InvariantCulture);
I'm partial to the second approach.
You need to use the Invariant Culture because there is no separator defined between the mm and tt in the format.
Why you are not using Regular Expression for this? ( http://msdn.microsoft.com/de-de/library/system.text.regularexpressions.regex(v=vs.80).aspx ) should be an easy thing for a Regex-Wizard (which I'm not)

Convert String to Datetime C#

I have a string value which is a datetime : "20100825161500" and I want to convert this to a System Datetime. I have tried Convert.ToDateTime and DateTime.Parse and these do not work.
You can use DateTime.ParseExact to pass the format you need.
Here is an example:
var parsed = DateTime.ParseExact("20100825161500","yyyyMMddHHmmss", null);
Possible format values are listed at
Standard Date and Time Format Strings and Custom Date and Time Format Strings
Try to use something like this
Datetime D = DateTime.ParseExact("20100825161500","yyyymmdd...",null)
here you have a link about how to make you "format" string
Because this string hasn't a format recognized by these 2 functions.
DateTime.Parse and Convert.ToDateTime require your string to be correctly formatted :
http://msdn.microsoft.com/en-us/library/1k1skd40.aspx
http://msdn.microsoft.com/en-us/library/xhz1w05e.aspx
You will have to write your custom parser for this kind of conversion

Categories

Resources