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.?

Recommended Answers

Do it this way:
[CODE]

    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 …
Jump to Post

All 7 Replies

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,
bye

commented: Great! +14

Thanks a lot for the reply...!!! This will work...!!!

Iam glad it did.
So you can close this thread by marking it as answered.

bye,ybe