PC Review


Reply
Thread Tools Rate Thread

DateTime.Parse - can't figure out how to make this work.

 
 
=?Utf-8?B?QnJpYW4gTmV3dHo=?=
Guest
Posts: n/a
 
      3rd Feb 2004
Hi everyone

I was just wondering if I'm going about this in the wrong way, since I can't get things to work correctly. I have an integer that I want to convert to a string, and then a DateTime. The string is in the format of "yyyyMMdd". I have the following

[start code

DateTimeFormatInfo dtfi = new DateTimeFormatInfo()
dtfi.LongDatePattern = "yyyyMMdd"

e.Item.Cells[1].Text = DateTime.Parse(Convert.ToString(r["run_date"]), dtfi).ToShortDateString()

[end code

The code fails on DateTime.Parse with this error

"String was not recognized as a valid DateTime.

I've verified that the Convert.ToString(r["run_date"]) part returns a string like "20040203". I've also tried setting the DateTimeFormatInfo's ShortDatePattern as well and that doesn't work either. What am I doing wrong

Thanks in advance
-Brian
 
Reply With Quote
 
 
 
 
=?Utf-8?B?SmF5c29uIEtuaWdodA==?=
Guest
Posts: n/a
 
      3rd Feb 2004
i could be wrong, but you need the slashes "/" between the parts of the date. i've had more luck with Convert.ToDateTime() than with DateTime.Parse, though they may actually do the exact same thing.

hth
jayson
 
Reply With Quote
 
mikeb
Guest
Posts: n/a
 
      4th Feb 2004
Brian Newtz wrote:
> Hi everyone!
>
> I was just wondering if I'm going about this in the wrong way, since I can't get things to work correctly. I have an integer that I want to convert to a string, and then a DateTime. The string is in the format of "yyyyMMdd". I have the following:
>
> [start code]
>
> DateTimeFormatInfo dtfi = new DateTimeFormatInfo();
> dtfi.LongDatePattern = "yyyyMMdd";
>
> e.Item.Cells[1].Text = DateTime.Parse(Convert.ToString(r["run_date"]), dtfi).ToShortDateString();
>
> [end code]
>
> The code fails on DateTime.Parse with this error:
>
> "String was not recognized as a valid DateTime."
>
> I've verified that the Convert.ToString(r["run_date"]) part returns a string like "20040203". I've also tried setting the DateTimeFormatInfo's ShortDatePattern as well and that doesn't work either. What am I doing wrong?
>


It seems that DateTime.Parse() does not handle date strings which have
no delimiters between the components of the date. If you change your
LongDatePattern to "yyyy MM dd" and change the actual format of your
date strings to have spaces between the parts, then your code works fine.

To get things to work with your non-delimited date strings, use
DateTime.ParseExact():

DateTime.ParseExact( Convert.ToString(r["run_date"]), "yyyyMMdd",
dtfi);

should work for you.

--
mikeb
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DateTime.Parse() vs DateTime.ParseExact() Peter Duniho Microsoft C# .NET 3 19th Jun 2007 04:19 PM
DateTime.Parse() js Microsoft ASP .NET 5 5th Oct 2006 12:38 AM
Weird CultureInfo - DateTime.Parse() and Decimal.Parse() Rico Microsoft C# .NET 8 22nd Sep 2006 11:59 AM
DateTime.Parse Paul Beckett Microsoft Dot NET 4 20th Feb 2004 05:40 PM
DateTime.Parse .. what's different in .NET 1.1 Atul Agarwal Microsoft Dot NET Framework 4 27th Aug 2003 08:23 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:23 PM.