简体   繁体   中英

How to get the datetime value from combo box

I have filled a combo box with the variables of long datetime, In combo i have shown only the Month names....

I want to get the datetime value corresponding to the selected month in combo. How can i do that?

If itmes are just month string you can't restore full datetime. The best you can do is to parse month string and construct a new DateTime based on the parsed month and constant year, day and time, like this:

var date = DateTime.ParseExact(dropDownList.SelectedItem.Text, "MMMM", CultureInfo.CurrentUICulture);

Replace CultureInfo.CurrentUICulture with the required culture if month name culture is different from UI culture.

You've taged the question asp.net, so i guess its a web app. Assuming the datetime value is in the value property and the month only in the text property of the ListItem

var dt = DateTime.Parse(DropDownList1.SelectedValue);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM