How can I parse this dynamic dropdown list to show only dd/mm/yyyy?

devnewbee
devnewbee used Ask the Experts™
on
Given the below list call and bind, how can I parse the dd output to be just dd/mm/yyyy?

      protected void Page_Load(object sender, EventArgs e)
        {
            DropDownList3.DataSource = this.GetQuarter(new System.DateTime(2008, 4, 10), 3);
            DropDownList3.DataBind();
        }

        public List<DateTime> GetQuarter(DateTime startDate, int years)
        {
            List<DateTime> lst = new List<DateTime>();
            DateTime endDate = startDate.AddYears(3);
            while (startDate < endDate)
            {
                lst.Add(startDate);
                startDate = startDate.AddMonths(3);
            }
            return lst;
        }
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
HainKurtSr. System Analyst

Commented:
try

lst.Add(startDate);
-->
lst.Add(String.Format("{0:dd/mm/yyyy}", startDate);

Author

Commented:
Error      1      The best overloaded method match for 'System.Collections.Generic.List<System.DateTime>.Add(System.DateTime)' has some invalid arguments

and

Error      2      Argument 1: cannot convert from 'string' to 'System.DateTime'
Most Valuable Expert 2012
Top Expert 2014

Commented:
You would need to change return type from List(of DateTime) to List(of String).
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

100% Solution : Try Using this code instead :
protected void Page_Load(object sender, EventArgs e)
        {
            List<DateTime> listDateTime = this.GetQuarter(new System.DateTime(2008, 4, 10), 3);
            DropDownList3.Items.Clear();             
            foreach(DateTime dt in listDateTime)
            {        

                 DropDownList3.Items.Add(DateTime.Parse(dt.ToShortDateString())); 
            }
        }

        public List<DateTime> GetQuarter(DateTime startDate, int years)
        {
            List<DateTime> lst = new List<DateTime>();
            DateTime endDate = startDate.AddYears(3);
            while (startDate < endDate)
            {
                lst.Add(startDate);
                startDate = startDate.AddMonths(3);
            }
            return lst;
        }

Open in new window

OR You Can Try This Code :
protected void Page_Load(object sender, EventArgs e)
        {
            List<DateTime> listDateTime = this.GetQuarter(new System.DateTime(2008, 4, 10), 3);
            DropDownList3.Items.Clear();             
            foreach(DateTime dt in listDateTime)
            {        

                 DropDownList3.Items.Add(dt.ToShortDateString()); 
            }
        }

        public List<DateTime> GetQuarter(DateTime startDate, int years)
        {
            List<DateTime> lst = new List<DateTime>();
            DateTime endDate = startDate.AddYears(3);
            while (startDate < endDate)
            {
                lst.Add(startDate);
                startDate = startDate.AddMonths(3);
            }
            return lst;
        }

Open in new window

Another Possibility would be to do this :
 
protected void Page_Load(object sender, EventArgs e)
        {
            List<DateTime> listDateTime = this.GetQuarter(new System.DateTime(2008, 4, 10), 3);
            List<string> listStrings = new List<string>();         
            foreach(DateTime dt in listDateTime)
            {        
                listStrings.Add(dt.ToShortDateString()); 
            }
            DropDownList3.DataSource = listStrings;
            DropDownList3.DataBind();
            
        }

        public List<DateTime> GetQuarter(DateTime startDate, int years)
        {
            List<DateTime> lst = new List<DateTime>();
            DateTime endDate = startDate.AddYears(3);
            while (startDate < endDate)
            {
                lst.Add(startDate);
                startDate = startDate.AddMonths(3);
            }
            return lst;
        }

Open in new window

If you wanna maintain the DropDownList's values as DateTime then use my 1st code snippet.. If you need those values stored as strings then use 2nd and 3rd code snippets respectively.. Again If u have any issues don`t worry I am still there to help you out.. C# is my favorite recipe :) Cheers!!

Author

Commented:
Thanks much!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial