|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
DateTime.ParseExact weird behaviorHere is the problem I discovered resently. Any idea how to go around?
in order to repeat behaviour the date has to be between 1 and 9. No problem with 2 digits date. Let's assume it is June 6, 2006 string s = DateTime.Now.ToString("MMMdyyyyhhmmtt"); // now s looks like "Jun620061220PM" // now trying to convert back the same string with the same format throws exception DateTime d = DateTime.ParseExact(s, "MMMdyyyyhhmmtt", whatever current culture is ...) does not make a lot of sense, does it? I mean if A=B that why B!=A ;))))))))))))))))))))))) What exception is thrown?
Probably the problem is that you don't have any separator after the day number, and as it can be either one or two digits it will read two digits if there are two digits, and throw an exception as "62" is not a valid day number. Try with a date format that has a fixed number of digits: "MMMddyyyyhhmmtt". Nick Chakarov wrote: Show quote > Here is the problem I discovered resently. Any idea how to go around? > > in order to repeat behaviour the date has to be between 1 and 9. No problem > with 2 digits date. > Let's assume it is June 6, 2006 > string s = DateTime.Now.ToString("MMMdyyyyhhmmtt"); > // now s looks like "Jun620061220PM" > // now trying to convert back the same string with the same format throws > exception > > DateTime d = DateTime.ParseExact(s, "MMMdyyyyhhmmtt", whatever current > culture is ...) > > does not make a lot of sense, does it? I mean if A=B that why B!=A > ;))))))))))))))))))))))) > > Exception is "invalid format".
The problem is that I am receiving datetime in this format as a string that I need to parse. But that is not the point. The point is that the formatting string should work back and forth. This is the point I am trying to make. Try it for yourself .... string s = DateTime.Now.ToString("MMMdyyyyhhmmtt"); System.Diagnostics.Debug.WriteLine(s); DateTime d = DateTime.ParseExact(s, "MMMdyyyyhhmmtt",System.Threading.Thread.CurrentThread.CurrentCulture); Show quote "Göran Andersson" <gu***@guffa.com> wrote in message news:uyWUXPhiGHA.4776@TK2MSFTNGP05.phx.gbl... > What exception is thrown? > > Probably the problem is that you don't have any separator after the day > number, and as it can be either one or two digits it will read two digits > if there are two digits, and throw an exception as "62" is not a valid day > number. > > Try with a date format that has a fixed number of digits: > "MMMddyyyyhhmmtt". > > Nick Chakarov wrote: >> Here is the problem I discovered resently. Any idea how to go around? >> >> in order to repeat behaviour the date has to be between 1 and 9. No >> problem with 2 digits date. >> Let's assume it is June 6, 2006 >> string s = DateTime.Now.ToString("MMMdyyyyhhmmtt"); >> // now s looks like "Jun620061220PM" >> // now trying to convert back the same string with the same format throws >> exception >> >> DateTime d = DateTime.ParseExact(s, "MMMdyyyyhhmmtt", whatever current >> culture is ...) >> >> does not make a lot of sense, does it? I mean if A=B that why B!=A >> ;))))))))))))))))))))))) nick wrote:
> Exception is "invalid format". http://lab.msdn.microsoft.com/productfeedback> The problem is that I am receiving datetime in this format as a > string that I need to parse. But that is not the point. > The point is that the formatting string should work back and forth. > This is the point I am trying to make. is where to make such points. I can easily undersstand why what you're trying to do doesn't work, and I can easily understand why you'd want it to work - I'd suggest reporting it as a bug. In the meantime, you'll have to do something else - like break the string apart yourself. -cd No, it shouldn't work back and forth. You can't expect any date format
to be parsable. If you for an example use the format "yMd", it could produce a string that looks like this: "2006112". This is a string that is impossible to parse as a date, as the date used to produce it could be either 2006-01-12 or 2006-11-02. You should check the length of the string. If it contains 14 characters, add a zero as the fourth character. Then you can use the "MMMddyyyyhhmmtt" format to parse it. nick wrote: Show quote > Exception is "invalid format". > The problem is that I am receiving datetime in this format as a string that > I need to parse. But that is not the point. > The point is that the formatting string should work back and forth. This is > the point I am trying to make. > Try it for yourself .... > string s = DateTime.Now.ToString("MMMdyyyyhhmmtt"); > > System.Diagnostics.Debug.WriteLine(s); > > DateTime d = DateTime.ParseExact(s, > "MMMdyyyyhhmmtt",System.Threading.Thread.CurrentThread.CurrentCulture); > > > > > "Göran Andersson" <gu***@guffa.com> wrote in message > news:uyWUXPhiGHA.4776@TK2MSFTNGP05.phx.gbl... >> What exception is thrown? >> >> Probably the problem is that you don't have any separator after the day >> number, and as it can be either one or two digits it will read two digits >> if there are two digits, and throw an exception as "62" is not a valid day >> number. >> >> Try with a date format that has a fixed number of digits: >> "MMMddyyyyhhmmtt". >> >> Nick Chakarov wrote: >>> Here is the problem I discovered resently. Any idea how to go around? >>> >>> in order to repeat behaviour the date has to be between 1 and 9. No >>> problem with 2 digits date. >>> Let's assume it is June 6, 2006 >>> string s = DateTime.Now.ToString("MMMdyyyyhhmmtt"); >>> // now s looks like "Jun620061220PM" >>> // now trying to convert back the same string with the same format throws >>> exception >>> >>> DateTime d = DateTime.ParseExact(s, "MMMdyyyyhhmmtt", whatever current >>> culture is ...) >>> >>> does not make a lot of sense, does it? I mean if A=B that why B!=A >>> ;))))))))))))))))))))))) > Hehe ;)
That's exactly what I am doing, but it is workaround. I would have expected more inteligent behavior from the parser in this particular case. For the example you are giving ... I agree ... But for this particula case .. no way ;) Show quote "Göran Andersson" <gu***@guffa.com> wrote in message news:udJ$masiGHA.4204@TK2MSFTNGP02.phx.gbl... > No, it shouldn't work back and forth. You can't expect any date format to > be parsable. > > If you for an example use the format "yMd", it could produce a string that > looks like this: "2006112". This is a string that is impossible to parse > as a date, as the date used to produce it could be either 2006-01-12 or > 2006-11-02. > > You should check the length of the string. If it contains 14 characters, > add a zero as the fourth character. Then you can use the "MMMddyyyyhhmmtt" > format to parse it. > > nick wrote: >> Exception is "invalid format". >> The problem is that I am receiving datetime in this format as a string >> that I need to parse. But that is not the point. >> The point is that the formatting string should work back and forth. This >> is the point I am trying to make. >> Try it for yourself .... >> string s = DateTime.Now.ToString("MMMdyyyyhhmmtt"); >> >> System.Diagnostics.Debug.WriteLine(s); >> >> DateTime d = DateTime.ParseExact(s, >> "MMMdyyyyhhmmtt",System.Threading.Thread.CurrentThread.CurrentCulture); >> >> >> >> >> "Göran Andersson" <gu***@guffa.com> wrote in message >> news:uyWUXPhiGHA.4776@TK2MSFTNGP05.phx.gbl... >>> What exception is thrown? >>> >>> Probably the problem is that you don't have any separator after the day >>> number, and as it can be either one or two digits it will read two >>> digits if there are two digits, and throw an exception as "62" is not a >>> valid day number. >>> >>> Try with a date format that has a fixed number of digits: >>> "MMMddyyyyhhmmtt". >>> >>> Nick Chakarov wrote: >>>> Here is the problem I discovered resently. Any idea how to go around? >>>> >>>> in order to repeat behaviour the date has to be between 1 and 9. No >>>> problem with 2 digits date. >>>> Let's assume it is June 6, 2006 >>>> string s = DateTime.Now.ToString("MMMdyyyyhhmmtt"); >>>> // now s looks like "Jun620061220PM" >>>> // now trying to convert back the same string with the same format >>>> throws exception >>>> >>>> DateTime d = DateTime.ParseExact(s, "MMMdyyyyhhmmtt", whatever current >>>> culture is ...) >>>> >>>> does not make a lot of sense, does it? I mean if A=B that why B!=A >>>> ;))))))))))))))))))))))) >> Yes, if the parser would be a bit smarter, it would realize that there
is only one of the format specifiers that has a variable length, and base the parsing around that specifier. The problem is that there is probably hundreds of special cases where some added logic would make the parser able to handle yet another situation. If all those were implemented, the parse method would contain huge amounts of code, and quite a few bugs. It would get pretty slow, and it would be nearly impossible to predict how it would react in different situations. Nick Chakarov wrote: Show quote > Hehe ;) > > That's exactly what I am doing, but it is workaround. I would have expected > more inteligent behavior from the parser in this particular case. For the > example you are giving ... I agree ... But for this particula case .. no > way ;) > > > "Göran Andersson" <gu***@guffa.com> wrote in message > news:udJ$masiGHA.4204@TK2MSFTNGP02.phx.gbl... >> No, it shouldn't work back and forth. You can't expect any date format to >> be parsable. >> >> If you for an example use the format "yMd", it could produce a string that >> looks like this: "2006112". This is a string that is impossible to parse >> as a date, as the date used to produce it could be either 2006-01-12 or >> 2006-11-02. >> >> You should check the length of the string. If it contains 14 characters, >> add a zero as the fourth character. Then you can use the "MMMddyyyyhhmmtt" >> format to parse it. >> >> nick wrote: >>> Exception is "invalid format". >>> The problem is that I am receiving datetime in this format as a string >>> that I need to parse. But that is not the point. >>> The point is that the formatting string should work back and forth. This >>> is the point I am trying to make. >>> Try it for yourself .... >>> string s = DateTime.Now.ToString("MMMdyyyyhhmmtt"); >>> >>> System.Diagnostics.Debug.WriteLine(s); >>> >>> DateTime d = DateTime.ParseExact(s, >>> "MMMdyyyyhhmmtt",System.Threading.Thread.CurrentThread.CurrentCulture); >>> >>> >>> >>> >>> "Göran Andersson" <gu***@guffa.com> wrote in message >>> news:uyWUXPhiGHA.4776@TK2MSFTNGP05.phx.gbl... >>>> What exception is thrown? >>>> >>>> Probably the problem is that you don't have any separator after the day >>>> number, and as it can be either one or two digits it will read two >>>> digits if there are two digits, and throw an exception as "62" is not a >>>> valid day number. >>>> >>>> Try with a date format that has a fixed number of digits: >>>> "MMMddyyyyhhmmtt". >>>> >>>> Nick Chakarov wrote: >>>>> Here is the problem I discovered resently. Any idea how to go around? >>>>> >>>>> in order to repeat behaviour the date has to be between 1 and 9. No >>>>> problem with 2 digits date. >>>>> Let's assume it is June 6, 2006 >>>>> string s = DateTime.Now.ToString("MMMdyyyyhhmmtt"); >>>>> // now s looks like "Jun620061220PM" >>>>> // now trying to convert back the same string with the same format >>>>> throws exception >>>>> >>>>> DateTime d = DateTime.ParseExact(s, "MMMdyyyyhhmmtt", whatever current >>>>> culture is ...) >>>>> >>>>> does not make a lot of sense, does it? I mean if A=B that why B!=A >>>>> ;))))))))))))))))))))))) > > |
|||||||||||||||||||||||