help with date formatting...  
Author Message
C#Student





PostPosted: Visual C# General, help with date formatting... Top

Hi,

Currently the below text box prints the date 12/13/2006. But it should be MM/DD/YYYY. Could some one please show me how to change the formatting. Thanks.

TextBox txtArrival = (TextBox) (e.Item.FindControl("txtArrivalDateAdd"));

if(txtArrival != null)

arrival = Convert.ToDateTime(txtArrival.Text);




Visual C#15  
 
 
Jeff Wharton





PostPosted: Visual C# General, help with date formatting... Top

I'm a little bit confused as 12/13/2006 is in MM/DD/YYYY format as there is no 13th Month!

 
 
Troy Magennis





PostPosted: Visual C# General, help with date formatting... Top

Hi,

The static methods you need to use are: DateTime.Parse or DateTime.ParseExact.

Here is the documentation on converting strings to dateTime values with links to examples can be found here:

http://msdn2.microsoft.com/en-us/library/2h3syy57.aspx

(or type DateTime.Parse, highlight it and hit F1.)

Troy Maagennis.


 
 
OmegaMan





PostPosted: Visual C# General, help with date formatting... Top

Look into using a calendar control (System.Windows.Forms.MonthCalendar) in-lieu of a textbox then you can do this instead of dealing with parsing.


System.Windows.Forms.MonthCalendar ArrivalCalendarDate;
...
txtArrival.Text = ArrivalCalendarDate.SelectionStart.ToShortDateString();

 


Keep in mind the datetime object has ToString() overriden so when you have that object (as shown above through dereferencing, it can output any format by format specifiers using to string


DateTime arrival = ArrivalCalendarDate.SelectionStart;

// Place it into the textbox without the slashes 12312007
txtArrival.Text = ArrivalCalendarDate.ToString("MMddyyyy");


 


For more info check out DateTime.ToString Method (IFormatProvider) in the documentation. Note to see that dynamically, in the editor place the I bar on ToString of the DateTime object and press F1.