Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

I have a DateTimePicker control.

I want only the date to be displayed, so I changed the format to "custom".
I want the date to be in dd/mm/yyyy format. I did this too but the issue is when I am putting the converted date in to the control it is throwing the error as "input string is not in correct format".

Solution for this please.




MIDL
dtpDate.Text = dr.ItemArray[1].ToString();
                        string n = DateFormat(dtpDate.Value.ToString());
                        dtpDate.Text = "";
                        dtpDate.Text = n.ToString();


C#
private string DateFormat(string dtp)
       {

           string date = dtpDate.Value.Day.ToString() + "/" + dtpDate.Value.Month.ToString() + "/" + dtpDate.Value.Year.ToString();
           //MessageBox.Show(date.ToString());
           return (date.ToString());
       }


C#
public void SetMyCustomFormat()
        {
            // Set the Format type and the CustomFormat string.
            dtpDate.Format = DateTimePickerFormat.Custom;
            dtpDate.CustomFormat = "dd/MM/yyy";
        }
Posted
Updated 6-Dec-10 21:43pm
v3
Comments
Abhinav S 7-Dec-10 2:14am    
You must post some code here. Without that its difficult to help you.
Dalek Dave 7-Dec-10 3:43am    
Edited for Grammar, Spelling, Syntax and Readability.

i think dtpDate.Text = dr.ItemArray[1].ToString(); is the issue on your code.
 
Share this answer
 
Comments
Dalek Dave 7-Dec-10 3:43am    
Good Call.
krishna kishore58 7-Dec-10 4:54am    
sorry 4 late reply... then wat it should be...
before you put the value on your datepicker, you should first check the value in correct date and change it into your own format.



example:
//dtpDate.Text = dr.ItemArray[1].ToString(); dont convert it to string.<br />
DateTime convertedDate = Convert.ToDateTime(dr.ItemArray[1].ToString())<br />
                        string n = DateFormat(convertedDate);<br />
                        dtpDate.Text = string.Empty;<br />
                        dtpDate.Text = n.ToString();<br />
<br />
<br />
private string DateFormat(DateTime dtp)<br />
       {<br />
           string date = dtpDate.Day.ToString() + "/" + dtpDate.Month.ToString() + "/" + dtpDate.Year.ToString();<br />
           //MessageBox.Show(date.ToString());<br />
           return (date.ToString());<br />
       }<br />
<br />
<br />
OR SIMPLEST WAY<br />
<br />
dtpDate.Text = Convert.ToDateTime(dr.ItemArray[1]).ToString("dd/MM/yyy")<br />
<br />
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900