PC Review


Reply
Thread Tools Rate Thread

Date Time and IFormatProvider

 
 
XmlAdoNewbie
Guest
Posts: n/a
 
      25th Feb 2004
Hello All,
I am trying to bind a date in the form of "20031218" to a datetime
object. I first try to convert the string using
Convert.ToDateTime("20031218") but i get an invalid date time
exception. I think I should be using the IFormatProvider in one of the
overloaded methods of Convert.ToDateTime but i can't find any
documentation that is easy to understand.. i am a newbie if you will.
Any code examples of how to do this would be great.. this is what i
have so far.

case "System.Windows.Forms.DateTimePicker":
if (ctrl.Name == dt.TableName + dc.ToString())
{
DataRow dr = ds.Tables[dt.TableName].Rows[0];
if (dr[dc].Equals(""))
{
dr[dc] = DateTime.Today;
}
else
{
dr[dc] = Convert.ToDateTime(dr[dc]); //this is where the problem
lies
}
ctrl.DataBindings.Add(new Binding("Value",ds.Table[
dt.TableName],dc.ColumnName));
}
break;

Thanks in advance!!
 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      25th Feb 2004
XmlAdoNewbie <(E-Mail Removed)> wrote:
> I am trying to bind a date in the form of "20031218" to a datetime
> object. I first try to convert the string using
> Convert.ToDateTime("20031218") but i get an invalid date time
> exception. I think I should be using the IFormatProvider in one of the
> overloaded methods of Convert.ToDateTime but i can't find any
> documentation that is easy to understand.. i am a newbie if you will.
> Any code examples of how to do this would be great.. this is what i
> have so far.


I suggest you use

DateTime.ParseExact (value, "yyyyMMdd", CultureInfo.InvariantCulture);

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
XmlAdoNewbie
Guest
Posts: n/a
 
      26th Feb 2004
Thanks So Much Jon, that works like a charm!!

Erin
 
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
Can I use an IFormatProvider to do this? 0to60 Microsoft Dot NET Framework 4 8th Jul 2008 05:18 PM
Can I use an IFormatProvider to do this? 0to60 Microsoft C# .NET 4 8th Jul 2008 05:18 PM
IFormatProvider Que Microsoft VB .NET 3 18th Sep 2006 08:41 PM
Calculating days & time left from start date/time to end date/time =?Utf-8?B?bWFyaWU=?= Microsoft Excel Worksheet Functions 7 7th Dec 2005 02:36 PM
iformatprovider for boolean =?Utf-8?B?RXJpYyBHb2ZvdGg=?= Microsoft VB .NET 1 19th Apr 2004 08:37 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:44 AM.