Avatar of devnewbee
devnewbee

asked on 

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

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;
        }
C#Visual Basic.NET.NET Programming

Avatar of undefined
Last Comment
devnewbee
Avatar of HainKurt
HainKurt
Flag of Canada image

try

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

ASKER

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'
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

You would need to change return type from List(of DateTime) to List(of String).
Avatar of starlite551
starlite551
Flag of India image

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

Avatar of starlite551
starlite551
Flag of India image

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

ASKER CERTIFIED SOLUTION
Avatar of starlite551
starlite551
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of devnewbee
devnewbee

ASKER

Thanks much!
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo