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.
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.
You could store it in a DateTime as follows:
DateTime expDate = new DateTime(ExpYear, ExpMonth, 1).AddMonths(1).AddDays(-1);
If it's for a credit card expiration date, make sure the day is the last day of the month or don't compare the day. Their may be some discrepancies on the last day being expired or not. It should be still valid so make sure the current date is at least a day greater.
You will need to save this value either as a nvarchar
, where you'll be able to do whatever you want, or a datetime
. The difference is that the datetime
format requires you to provide the day, and the time be set to midnight. The value 1, for the first day of the month should be considered here.
DateTime Today = new DateTime( DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(1).AddDays(-1);
DateTime cc = new DateTime(2016, 9, 1).AddMonths(1).AddDays(-1);
Console.WriteLine(Today.ToString());
Console.WriteLine(cc.ToString());
if (Today <= cc)
{
Console.WriteLine("Ok");
}
else
{
Console.WriteLine("Card Expiry Date is not valid ");
}
精彩评论