Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datetime format for a month and year

Tags:

c#

asp.net

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.

like image 259
challengeAccepted Avatar asked Aug 18 '10 15:08

challengeAccepted


1 Answers

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.

like image 133
Jon Skeet Avatar answered Sep 21 '22 09:09

Jon Skeet