Date convert issue - c#

I try to convert below text to date, for more than 80 cases from 2011 to 2017-July it is working fine but from Aug it converts the year to the year 2018 and considers the number as a day. I want to check is there any way to convert format date exactly and properly? this piece of code is for data migration so if it work wrongly it is disaster situation.
Convert.ToDateTime("Sep-17")
The answer in my machine is: {9/17/2018 12:00:00 AM}
the correct answer should be: {9/1/2017 12:00:00 AM}

DateTime.ParseExact("Sep-17", "MMM-yy", CultureInfo.InvariantCulture);

Related

String was not recognized as a valid DateTime because the day of week was incorrect

i have this simple line of code :
DateTime.ParseExact("Mon 7:00 PM", "ddd h:mm tt", CultureInfo.InvariantCulture)
If you run this code, 6 days in a week,it will throw this exception :
System.FormatException: String was not recognized as a valid DateTime because the day of week was incorrect.
If you run it on Monday, it will run OK. Is there any way to parse that string correctly ?
Unfortunately, DateTime.Parse(Exact) will always return a complete DateTime, complete with a date.
If you ommit any parts detailing the date, it will default to todays date. 6 out of 7 days each week, that will not match the pattern that states monday, and that's why you get that exception.
ParseExact will not attempt to find a matching DateTime value.
For instance, if you run this:
ParseExact("Mon 30.03", "ddd dd.MM", culture)
it will work this year, because 30th of March 2020 is a Monday. However, in 2021, 30th of a March will be on a Tuesday, and thus it will fail again. ParseExact will not try to find a matching year that would have 30th of March on a Monday, and it's the same thing with just specifying Monday. It will not try to figure out which Monday you're talking about.
In short, you will need to find a different way of doing this.
I guess you should step back from ParseExact, and ask yourself, how exactly should "Mon 30.03" be translated into a specific date in a specific year. What is the logic. Then you can try to find the right method to call, or most likely, write the code to do it.

Dateime Parse method adding one day to input date

I am reading a date from an xml file and parsing it to a my desired format. It i adding a day to the date and i cant seem to figure out why.
input : 2014-02-12T15:21:19-08:00
output : 13 Feb 2014 01:21
Here is my code to parse date:
string date = DateTime.Parse(row["CountDate"].ToString()).ToString("dd MMM yyyy HH:mm");
Any help will be greatly appreciated.
The reason is that the timezone information is being used to adjust the time to your local time zone.
If you remove the "-08:00" suffix, you'll find that the time won't be adjusted. However, you need to know whether the timezone information is important before ignoring it!
Well it looks like what you have is a UTC date/time with an 8 hour offset, when you parse the date what you have is an instance of the local time (Parse will take into account the offset).
If you are only interested in the UTC date/time then you can parse just that particular information
DateTime.ParseExact("yyyy-MM-ddTHH:mm:ss", row["CountDate"],
CultureInfo.InvariantCulture);

converting a string to date in the exact manner

I have a string "11 Jan 2011" which I want to convert to the datatype date (i.e 11 Jan 2011).
I have tried all resources about datetime.parse, datetime.parse exact but all these things gives me the same output 2011/01/11 12:00:00 AM. I really don't understand this behaviour. I tried the following:
1.DateTime date = DateTime.Parse("11 Jan 2011");
2.DateTime date = DateTime.ParseExact("11 Jan 2011" , #"dd MMM yyyy", System.Globalization.CultureInfo.InvariantCulture);
parsing and displaying are not the same thing
you parse the original string to a DateTime object but display results using Date/Time format strings
Both your calls are correct.
A DateTime structure preserves no information about formatting; it just represents the raw date and time.
What you need to do is ensure that when you display your date, you do so in the correct format - e.g. by calling string displayString = date.ToString("dd MMM yyyy");

Troubles parsing DateTime from string

I am currently trying to parse a string that is obtained from an xml that is downloaded from the web every few minutes. The string looks like this:
Thu Jul 12 08:39:56 GMT+0100 2012
At first I just did a string.split and took out everything after the time (GMT+0100 2012) and inserted 2012 after the date.
This worked great until the date changed to:
Thu Jul 12 08:39:56 GMT+0000 2012
So I would like to dynamically pasre the GMT+ whatever as they send me that string in c#.
Any advice would be appreciated.
You can use DateTime.ParseExact with a custom date and time format string:
DateTime.ParseExact("Thu Jul 12 08:39:56 GMT+0000 2012",
"ddd MMM dd hh:mm:ss 'GMT'K yyyy",
CultureInfo.InvariantCulture)
This will throw a format exception if the string and format string do not match exactly, so you may want to use DateTime.TryParseExact that will return a false if it fails.
Instead of DateTime you may want to use DateTimeOffset that preserved timezone information , as #Keith commented - this may be important to your application.
Two things you can do: First, you should be able to use a custom format string with a ParseExact method, either from DateTime or DateTimeOffset (I would use DateTimeOffset if the actual time zone of the stamp is important, and not just the equivalent time in UTC or your local time zone).
Have a look: DateTime custom format string
The format string would probably be something like #"ddd MMM dd HH:mm:ss 'GMT'zzzz yyyy".
However, there's one snag; the .NET time zone offset ("zzzz" or simply "K") always includes a colon between the hour and minute when expressed as a string, which your input strings do not have. There is no way I know of to specify that the time zone offset doesn't/shouldn't have this colon, and I'm pretty sure that trying to parse it without a colon would cause an error.
The simplest workaround is to remove that specific colon from the string prior to parsing it. The code for that given your input is simply to remove the last colon character in the string:
var updatedString = inputString.Remove(inputString.LastIndexOf(':'), 1);
Try DateTime.Parse method to parse your date.
This should work:
XmlConvert.ToDateTime(textBox1.Text, "ddd MMM dd HH:mm:ss 'GMT'zzzz yyyy");

Getting exception when using DateTime.Parse method

So, i have this string "Date: Mon Jan 03 2011 19:29:44 GMT+0200", and when i use DateTime.Parse(date).ToString(); i'm getting "String was not recognized as a valid DateTime."
If i remove the '+0200' part it works ok, but ofcourse it doesn't show the correct local time.
What's wrong with that?
From the documentation, it seems that DateTime.Parse() only understands:
The GMT designator, used alone, e.g. Mon, Jan 03 2011 17:29:44 GMT, or
A time zone offset specified without the GMT designator, e.g. Mon, Jan 03 2011 19:29:44+02:00.
You might want to convert your date string to the second form.
It just means that the time zone offset isn't an expected part of the default format strings.
If you know what format you're expecting, I suggest you call DateTime.ParseExact (or DateTime.TryParseExact), specifying the format(s) to try. Look at the documentation for custom date/time format strings for more details.
You have two mistakes.
First - don`t use Parse method. More correct is TryParse.
Second - you will have globalisation issues, when you use Parse or TryParse without arguments.
For example, see this code:
DateTime.Parse( "01.02.2011" ); In the USA it is 2nd of January. In the Germany it is 1st of February.
So, I recomment you to use formats from this article.

Categories

Resources