|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Parse date with format yyyyMMddDear all,
i have a date in the format yyyyMMdd. When I try to parse this I get the following error: string was not recognized as a valid datetime. I use the following code: CultureInfo culture = new CultureInfo("en-US"); culture.DateTimeFormat.DateSeparator = string.Empty; culture.DateTimeFormat.ShortDatePattern = "yyyyMMdd"; // Here the error occurs: DateTime date = DateTime.Parse("20041123", culture.DateTimeFormat); Thanx, René Hey René,
Try this instead: CultureInfo nfo = new CultureInfo("en-US"); DateTime d = DateTime.ParseExact("20041123", "yyyyMMdd", nfo); HTH, Jakob. Show quoteHide quote "René Titulaer" wrote: > Dear all, > > i have a date in the format yyyyMMdd. When I try to parse this I get the > following error: string was not recognized as a valid datetime. > > I use the following code: > > CultureInfo culture = new CultureInfo("en-US"); > culture.DateTimeFormat.DateSeparator = string.Empty; > culture.DateTimeFormat.ShortDatePattern = "yyyyMMdd"; > > // Here the error occurs: > DateTime date = DateTime.Parse("20041123", culture.DateTimeFormat); > > Thanx, > René > Thanx,
this works. To be honest this was not really my problem but I did try to understand date formatting and then I ran into this problem. Can you please look at my original problem. I have a xml with a date. When I read it into a dataset with ReadXML I get a datetime conversion error (the column is defined as xs:date in the dataset schema). The format is "yyyyMMdd". I did try to solve the error by setting the cultureinfo of the current threat, my code: CultureInfo culture = new CultureInfo("en-US", false); culture.DateTimeFormat.DateSeparator = string.Empty; culture.DateTimeFormat.ShortDatePattern = "yyyyMMdd"; culture.DateTimeFormat.LongDatePattern = "yyyyMMdd"; System.Threading.Thread.CurrentThread.CurrentCulture = culture; System.Threading.Thread.CurrentThread.CurrentUICulture = culture; taskdata.ReadXml(reader, XmlReadMode.Auto); Thanks, René Show quoteHide quote "Jakob Christensen" wrote: > Hey René, > > Try this instead: > > CultureInfo nfo = new CultureInfo("en-US"); > DateTime d = DateTime.ParseExact("20041123", "yyyyMMdd", nfo); > > HTH, Jakob. > > "René Titulaer" wrote: > > > Dear all, > > > > i have a date in the format yyyyMMdd. When I try to parse this I get the > > following error: string was not recognized as a valid datetime. > > > > I use the following code: > > > > CultureInfo culture = new CultureInfo("en-US"); > > culture.DateTimeFormat.DateSeparator = string.Empty; > > culture.DateTimeFormat.ShortDatePattern = "yyyyMMdd"; > > > > // Here the error occurs: > > DateTime date = DateTime.Parse("20041123", culture.DateTimeFormat); > > > > Thanx, > > René > > As far as I know, the format of the dates in the XML must be 'yyyy-MM-dd'.
Otherwise you can not use ReadXml. Are you generating the XML yourself? Regards, Jakob. Show quoteHide quote "René Titulaer" wrote: > Thanx, > > this works. > > To be honest this was not really my problem but I did try to understand date > formatting and then I ran into this problem. > > Can you please look at my original problem. I have a xml with a date. When I > read it into a dataset with ReadXML I get a datetime conversion error (the > column is defined as xs:date in the dataset schema). The format is > "yyyyMMdd". I did try to solve the error by setting the cultureinfo of the > current threat, my code: > > CultureInfo culture = new CultureInfo("en-US", false); > culture.DateTimeFormat.DateSeparator = string.Empty; > culture.DateTimeFormat.ShortDatePattern = "yyyyMMdd"; > culture.DateTimeFormat.LongDatePattern = "yyyyMMdd"; > > System.Threading.Thread.CurrentThread.CurrentCulture = culture; > System.Threading.Thread.CurrentThread.CurrentUICulture = culture; > > taskdata.ReadXml(reader, XmlReadMode.Auto); > > Thanks, > René > > "Jakob Christensen" wrote: > > > Hey René, > > > > Try this instead: > > > > CultureInfo nfo = new CultureInfo("en-US"); > > DateTime d = DateTime.ParseExact("20041123", "yyyyMMdd", nfo); > > > > HTH, Jakob. > > > > "René Titulaer" wrote: > > > > > Dear all, > > > > > > i have a date in the format yyyyMMdd. When I try to parse this I get the > > > following error: string was not recognized as a valid datetime. > > > > > > I use the following code: > > > > > > CultureInfo culture = new CultureInfo("en-US"); > > > culture.DateTimeFormat.DateSeparator = string.Empty; > > > culture.DateTimeFormat.ShortDatePattern = "yyyyMMdd"; > > > > > > // Here the error occurs: > > > DateTime date = DateTime.Parse("20041123", culture.DateTimeFormat); > > > > > > Thanx, > > > René > > > No, the xml comes from the back-office.
I'm not sure if a date always have to be something with a '-'. I try to translate it to another problem: If I want to do: DateTime.Parse("20041123") Do you think that it should work by changing the current culture: CultureInfo culture = new CultureInfo("en-US", false); culture.DateTimeFormat.DateSeparator = string.Empty; culture.DateTimeFormat.ShortDatePattern = "yyyyMMdd"; culture.DateTimeFormat.LongDatePattern = "yyyyMMdd"; System.Threading.Thread.CurrentThread.CurrentCulture = culture; System.Threading.Thread.CurrentThread.CurrentUICulture = culture; DateTime.Parse("20041123") This should work, don't you think (for me it doesn't but why)? Next, if this works and readXML doesn't then I'm sure readXML needs a seperator. Hope you find some time to share your thoughts, René Show quoteHide quote "Jakob Christensen" wrote: > As far as I know, the format of the dates in the XML must be 'yyyy-MM-dd'. > Otherwise you can not use ReadXml. Are you generating the XML yourself? > > Regards, Jakob. > > > "René Titulaer" wrote: > > > Thanx, > > > > this works. > > > > To be honest this was not really my problem but I did try to understand date > > formatting and then I ran into this problem. > > > > Can you please look at my original problem. I have a xml with a date. When I > > read it into a dataset with ReadXML I get a datetime conversion error (the > > column is defined as xs:date in the dataset schema). The format is > > "yyyyMMdd". I did try to solve the error by setting the cultureinfo of the > > current threat, my code: > > > > CultureInfo culture = new CultureInfo("en-US", false); > > culture.DateTimeFormat.DateSeparator = string.Empty; > > culture.DateTimeFormat.ShortDatePattern = "yyyyMMdd"; > > culture.DateTimeFormat.LongDatePattern = "yyyyMMdd"; > > > > System.Threading.Thread.CurrentThread.CurrentCulture = culture; > > System.Threading.Thread.CurrentThread.CurrentUICulture = culture; > > > > taskdata.ReadXml(reader, XmlReadMode.Auto); > > > > Thanks, > > René > > > > "Jakob Christensen" wrote: > > > > > Hey René, > > > > > > Try this instead: > > > > > > CultureInfo nfo = new CultureInfo("en-US"); > > > DateTime d = DateTime.ParseExact("20041123", "yyyyMMdd", nfo); > > > > > > HTH, Jakob. > > > > > > "René Titulaer" wrote: > > > > > > > Dear all, > > > > > > > > i have a date in the format yyyyMMdd. When I try to parse this I get the > > > > following error: string was not recognized as a valid datetime. > > > > > > > > I use the following code: > > > > > > > > CultureInfo culture = new CultureInfo("en-US"); > > > > culture.DateTimeFormat.DateSeparator = string.Empty; > > > > culture.DateTimeFormat.ShortDatePattern = "yyyyMMdd"; > > > > > > > > // Here the error occurs: > > > > DateTime date = DateTime.Parse("20041123", culture.DateTimeFormat); > > > > > > > > Thanx, > > > > René > > > > No, I am afraid it can not be done that way.
DateTimeFormatInfo.ShortDatePattern is used only for outputting and not for parsing. But it is true that DateTime.Parse will use the current culture if nothing else has been specified. The method DateTimeFormatInfo.GetAllDateTimePatterns gives you the supported patterns for a culture. I created a small piece of code that ran through all cultures and all patterns, and none of them matched 'yyyyMMdd'. Do you know for a fact that ReadXml uses DateTime.Parse? Regards, Jakob. Show quoteHide quote "René Titulaer" wrote: > No, the xml comes from the back-office. > > I'm not sure if a date always have to be something with a '-'. > > I try to translate it to another problem: > > If I want to do: > DateTime.Parse("20041123") > > Do you think that it should work by changing the current culture: > > CultureInfo culture = new CultureInfo("en-US", false); > culture.DateTimeFormat.DateSeparator = string.Empty; > culture.DateTimeFormat.ShortDatePattern = "yyyyMMdd"; > culture.DateTimeFormat.LongDatePattern = "yyyyMMdd"; > > System.Threading.Thread.CurrentThread.CurrentCulture = culture; > System.Threading.Thread.CurrentThread.CurrentUICulture = culture; > > DateTime.Parse("20041123") > > This should work, don't you think (for me it doesn't but why)? Next, if this > works and readXML doesn't then I'm sure readXML needs a seperator. > > Hope you find some time to share your thoughts, > René > > "Jakob Christensen" wrote: > > > As far as I know, the format of the dates in the XML must be 'yyyy-MM-dd'. > > Otherwise you can not use ReadXml. Are you generating the XML yourself? > > > > Regards, Jakob. > > > > > > "René Titulaer" wrote: > > > > > Thanx, > > > > > > this works. > > > > > > To be honest this was not really my problem but I did try to understand date > > > formatting and then I ran into this problem. > > > > > > Can you please look at my original problem. I have a xml with a date. When I > > > read it into a dataset with ReadXML I get a datetime conversion error (the > > > column is defined as xs:date in the dataset schema). The format is > > > "yyyyMMdd". I did try to solve the error by setting the cultureinfo of the > > > current threat, my code: > > > > > > CultureInfo culture = new CultureInfo("en-US", false); > > > culture.DateTimeFormat.DateSeparator = string.Empty; > > > culture.DateTimeFormat.ShortDatePattern = "yyyyMMdd"; > > > culture.DateTimeFormat.LongDatePattern = "yyyyMMdd"; > > > > > > System.Threading.Thread.CurrentThread.CurrentCulture = culture; > > > System.Threading.Thread.CurrentThread.CurrentUICulture = culture; > > > > > > taskdata.ReadXml(reader, XmlReadMode.Auto); > > > > > > Thanks, > > > René > > > > > > "Jakob Christensen" wrote: > > > > > > > Hey René, > > > > > > > > Try this instead: > > > > > > > > CultureInfo nfo = new CultureInfo("en-US"); > > > > DateTime d = DateTime.ParseExact("20041123", "yyyyMMdd", nfo); > > > > > > > > HTH, Jakob. > > > > > > > > "René Titulaer" wrote: > > > > > > > > > Dear all, > > > > > > > > > > i have a date in the format yyyyMMdd. When I try to parse this I get the > > > > > following error: string was not recognized as a valid datetime. > > > > > > > > > > I use the following code: > > > > > > > > > > CultureInfo culture = new CultureInfo("en-US"); > > > > > culture.DateTimeFormat.DateSeparator = string.Empty; > > > > > culture.DateTimeFormat.ShortDatePattern = "yyyyMMdd"; > > > > > > > > > > // Here the error occurs: > > > > > DateTime date = DateTime.Parse("20041123", culture.DateTimeFormat); > > > > > > > > > > Thanx, > > > > > René > > > > > Jakob,
thanks again for your response. I think you're right, all the patterns you can set influence the display not the parsing. Strange, I think, that you can't influence the parsing. ReadXML isn't actually using datetime.parse but XMLConvert.ToDateTime. This one does work when I use the format attribute: DateTime dt = XmlConvert.ToDateTime("20041119", "yyyyMMdd"); But when I use dataset.readxml it doesn't work because it uses XmlConvert.ToDateTime("20041119"); There is no way I can add a parameter. So now I'm back where I started: can I add a custom date pattern to DateTimeFormat (and then use this the current culture). Regards, René Show quoteHide quote "Jakob Christensen" wrote: > No, I am afraid it can not be done that way. > DateTimeFormatInfo.ShortDatePattern is used only for outputting and not for > parsing. > > But it is true that DateTime.Parse will use the current culture if nothing > else has been specified. The method > DateTimeFormatInfo.GetAllDateTimePatterns gives you the supported patterns > for a culture. I created a small piece of code that ran through all cultures > and all patterns, and none of them matched 'yyyyMMdd'. > > Do you know for a fact that ReadXml uses DateTime.Parse? > > Regards, Jakob. > > > "René Titulaer" wrote: > > > No, the xml comes from the back-office. > > > > I'm not sure if a date always have to be something with a '-'. > > > > I try to translate it to another problem: > > > > If I want to do: > > DateTime.Parse("20041123") > > > > Do you think that it should work by changing the current culture: > > > > CultureInfo culture = new CultureInfo("en-US", false); > > culture.DateTimeFormat.DateSeparator = string.Empty; > > culture.DateTimeFormat.ShortDatePattern = "yyyyMMdd"; > > culture.DateTimeFormat.LongDatePattern = "yyyyMMdd"; > > > > System.Threading.Thread.CurrentThread.CurrentCulture = culture; > > System.Threading.Thread.CurrentThread.CurrentUICulture = culture; > > > > DateTime.Parse("20041123") > > > > This should work, don't you think (for me it doesn't but why)? Next, if this > > works and readXML doesn't then I'm sure readXML needs a seperator. > > > > Hope you find some time to share your thoughts, > > René > > > > "Jakob Christensen" wrote: > > > > > As far as I know, the format of the dates in the XML must be 'yyyy-MM-dd'. > > > Otherwise you can not use ReadXml. Are you generating the XML yourself? > > > > > > Regards, Jakob. > > > > > > > > > "René Titulaer" wrote: > > > > > > > Thanx, > > > > > > > > this works. > > > > > > > > To be honest this was not really my problem but I did try to understand date > > > > formatting and then I ran into this problem. > > > > > > > > Can you please look at my original problem. I have a xml with a date. When I > > > > read it into a dataset with ReadXML I get a datetime conversion error (the > > > > column is defined as xs:date in the dataset schema). The format is > > > > "yyyyMMdd". I did try to solve the error by setting the cultureinfo of the > > > > current threat, my code: > > > > > > > > CultureInfo culture = new CultureInfo("en-US", false); > > > > culture.DateTimeFormat.DateSeparator = string.Empty; > > > > culture.DateTimeFormat.ShortDatePattern = "yyyyMMdd"; > > > > culture.DateTimeFormat.LongDatePattern = "yyyyMMdd"; > > > > > > > > System.Threading.Thread.CurrentThread.CurrentCulture = culture; > > > > System.Threading.Thread.CurrentThread.CurrentUICulture = culture; > > > > > > > > taskdata.ReadXml(reader, XmlReadMode.Auto); > > > > > > > > Thanks, > > > > René > > > > > > > > "Jakob Christensen" wrote: > > > > > > > > > Hey René, > > > > > > > > > > Try this instead: > > > > > > > > > > CultureInfo nfo = new CultureInfo("en-US"); > > > > > DateTime d = DateTime.ParseExact("20041123", "yyyyMMdd", nfo); > > > > > > > > > > HTH, Jakob. > > > > > > > > > > "René Titulaer" wrote: > > > > > > > > > > > Dear all, > > > > > > > > > > > > i have a date in the format yyyyMMdd. When I try to parse this I get the > > > > > > following error: string was not recognized as a valid datetime. > > > > > > > > > > > > I use the following code: > > > > > > > > > > > > CultureInfo culture = new CultureInfo("en-US"); > > > > > > culture.DateTimeFormat.DateSeparator = string.Empty; > > > > > > culture.DateTimeFormat.ShortDatePattern = "yyyyMMdd"; > > > > > > > > > > > > // Here the error occurs: > > > > > > DateTime date = DateTime.Parse("20041123", culture.DateTimeFormat); > > > > > > > > > > > > Thanx, > > > > > > René > > > > > > René,
According to the XML Schema, dates in XML must be specified in the format 'yyyy-MM-yy' (http://www.w3.org/TR/xmlschema-2/#date). I recommend not using datasets for this if you can not control the XML (actually, I hardly ever recommend datasets but that's a different story). If you parse the XML manually, you gain greater control. Regards, Jakob. Show quoteHide quote "René Titulaer" wrote: > Jakob, > > thanks again for your response. I think you're right, all the patterns you > can set influence the display not the parsing. Strange, I think, that you > can't influence the parsing. > > ReadXML isn't actually using datetime.parse but XMLConvert.ToDateTime. > This one does work when I use the format attribute: > DateTime dt = XmlConvert.ToDateTime("20041119", "yyyyMMdd"); > > But when I use dataset.readxml it doesn't work because it uses > XmlConvert.ToDateTime("20041119"); > There is no way I can add a parameter. > > So now I'm back where I started: can I add a custom date pattern to > DateTimeFormat (and then use this the current culture). > > Regards, > René > > > > "Jakob Christensen" wrote: > > > No, I am afraid it can not be done that way. > > DateTimeFormatInfo.ShortDatePattern is used only for outputting and not for > > parsing. > > > > But it is true that DateTime.Parse will use the current culture if nothing > > else has been specified. The method > > DateTimeFormatInfo.GetAllDateTimePatterns gives you the supported patterns > > for a culture. I created a small piece of code that ran through all cultures > > and all patterns, and none of them matched 'yyyyMMdd'. > > > > Do you know for a fact that ReadXml uses DateTime.Parse? > > > > Regards, Jakob. > > > > > > "René Titulaer" wrote: > > > > > No, the xml comes from the back-office. > > > > > > I'm not sure if a date always have to be something with a '-'. > > > > > > I try to translate it to another problem: > > > > > > If I want to do: > > > DateTime.Parse("20041123") > > > > > > Do you think that it should work by changing the current culture: > > > > > > CultureInfo culture = new CultureInfo("en-US", false); > > > culture.DateTimeFormat.DateSeparator = string.Empty; > > > culture.DateTimeFormat.ShortDatePattern = "yyyyMMdd"; > > > culture.DateTimeFormat.LongDatePattern = "yyyyMMdd"; > > > > > > System.Threading.Thread.CurrentThread.CurrentCulture = culture; > > > System.Threading.Thread.CurrentThread.CurrentUICulture = culture; > > > > > > DateTime.Parse("20041123") > > > > > > This should work, don't you think (for me it doesn't but why)? Next, if this > > > works and readXML doesn't then I'm sure readXML needs a seperator. > > > > > > Hope you find some time to share your thoughts, > > > René > > > > > > "Jakob Christensen" wrote: > > > > > > > As far as I know, the format of the dates in the XML must be 'yyyy-MM-dd'. > > > > Otherwise you can not use ReadXml. Are you generating the XML yourself? > > > > > > > > Regards, Jakob. > > > > > > > > > > > > "René Titulaer" wrote: > > > > > > > > > Thanx, > > > > > > > > > > this works. > > > > > > > > > > To be honest this was not really my problem but I did try to understand date > > > > > formatting and then I ran into this problem. > > > > > > > > > > Can you please look at my original problem. I have a xml with a date. When I > > > > > read it into a dataset with ReadXML I get a datetime conversion error (the > > > > > column is defined as xs:date in the dataset schema). The format is > > > > > "yyyyMMdd". I did try to solve the error by setting the cultureinfo of the > > > > > current threat, my code: > > > > > > > > > > CultureInfo culture = new CultureInfo("en-US", false); > > > > > culture.DateTimeFormat.DateSeparator = string.Empty; > > > > > culture.DateTimeFormat.ShortDatePattern = "yyyyMMdd"; > > > > > culture.DateTimeFormat.LongDatePattern = "yyyyMMdd"; > > > > > > > > > > System.Threading.Thread.CurrentThread.CurrentCulture = culture; > > > > > System.Threading.Thread.CurrentThread.CurrentUICulture = culture; > > > > > > > > > > taskdata.ReadXml(reader, XmlReadMode.Auto); > > > > > > > > > > Thanks, > > > > > René > > > > > > > > > > "Jakob Christensen" wrote: > > > > > > > > > > > Hey René, > > > > > > > > > > > > Try this instead: > > > > > > > > > > > > CultureInfo nfo = new CultureInfo("en-US"); > > > > > > DateTime d = DateTime.ParseExact("20041123", "yyyyMMdd", nfo); > > > > > > > > > > > > HTH, Jakob. > > > > > > > > > > > > "René Titulaer" wrote: > > > > > > > > > > > > > Dear all, > > > > > > > > > > > > > > i have a date in the format yyyyMMdd. When I try to parse this I get the > > > > > > > following error: string was not recognized as a valid datetime. > > > > > > > > > > > > > > I use the following code: > > > > > > > > > > > > > > CultureInfo culture = new CultureInfo("en-US"); > > > > > > > culture.DateTimeFormat.DateSeparator = string.Empty; > > > > > > > culture.DateTimeFormat.ShortDatePattern = "yyyyMMdd"; > > > > > > > > > > > > > > // Here the error occurs: > > > > > > > DateTime date = DateTime.Parse("20041123", culture.DateTimeFormat); > > > > > > > > > > > > > > Thanx, > > > > > > > René > > > > > > >
Other interesting topics
Re-flunk-tion!
ASP.NET 2.0 Maintain Scroll Position on PostBack with Mozilla FIREFOX in the works? Monitor (lock) storing the username and password information Installing VIsual Studio 2002 and Visual Studio 2003 on the same PC FastGetExistingType() method question? confusing remark in MSDN Control.BeginInvoke() doc Version of .Net.... .net framework 1.1 installation on Windows NT 4 with SBS RE: is there an instruction in VB.NET |
|||||||||||||||||||||||