Ram Ram - 2 months ago 13
ASP.NET (C#) Question

Datetime format for a month and year

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.

Answer

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.