Alex Rivera | Logout

Datetime format for a month and year

Asked 2010-08-18T15:24:38.680
10
DateTime ExpMonth = Convert.ToInt32(ddExpMonth); ---- DropDown(user selects a month)
DateTime ExpYear = Convert.ToInt32(ddExpYear);   ---- Dropdown(user selects year)

Datetime ExpDate = ///// I want this part to be saved as Datetime  02/2012

How is this possible. Or any other way.

Edit
Report

1 Answer

18

A DateTime value doesn't know about a format - it's just a date and a time. You can create a new DateTime value with the relevant information:

DateTime expiry = new DateTime(Convert.ToInt32(ddExpYear),
                               Convert.ToInt32(ddExpMonth),
                               1);

... but how that is "saved" is entirely up to you. If you give us more information, we may be able to help you more. You can format it to a string easily enough:

string formatted = expiry.ToString("yyyy/MM");

... but that may not be what you're after.

answered 2010-08-18T15:27:14.077

Your Answer