Date and time format for 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 in any other way.

+6
source share
4 answers

Value A DateTime does not know about the format - it is just a date and 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 it is "saved" is entirely up to you. If you give us more information, we can help you more. You can easily format it to a line:

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

... but it may not be like you.

+11
source share

You can save it in DateTime as follows:

 DateTime expDate = new DateTime(ExpYear, ExpMonth, 1).AddMonths(1).AddDays(-1); 

If this is for the credit card expiration date, make sure the day is the last day of the month or not comparing the day. There may be some inconsistencies on the last day, expired or not. It should remain valid, so make sure the current date is at least a day longer.

+3
source share

You need to save this value either as nvarchar , where you can do whatever you want, or datetime . The difference is that the datetime format requires you to provide a day, and the time should be set at midnight. A value of 1 for the first day of the month should be considered here.

0
source share
  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 "); } 
0
source share

All Articles