Hi, I need that user to type a date in a text box in yyyy/MM/dd format and then popup a calendar from the typed date. But to parse the date into calendar the whole date should be typed.otherwise it gives date conversion error. I need as an example;-

* if user typed only year then it should be "yyyy/01/01"
* if user type year and month then "yyyy/MM/01" like that.

Assume that user has typed only year(2011). how can i convert a string like "2011" to "yyyy/MM/dd" format.?

Thank You. I can't use a date time picker here. I need to give a manual date typing option in a text box and then popup a calendar.

Do it this way:

private void button1_Click(object sender, EventArgs e)
            DateTime date = DateTime.MinValue;
            if (!DateTime.TryParse(textBox1.Text, out date))
                string[] strSplit = textBox1.Text.Split('/');
                if (strSplit.Length == 1)
                    date = new DateTime(int.Parse(strSplit[0]), 1, 1);
                else if (strSplit.Length == 2)
                    date = new DateTime(int.Parse(strSplit[0]), int.Parse(strSplit[1]), 1);
            MessageBox.Show("Real date time is " + date);

Now you use "date" variable where ever you need to, or to set dateTimePicker control (to set date), or what ever.

Hope it helps,

Edited 5 Years Ago by Mitja Bonca: n/a

This question has already been answered. Start a new discussion instead.