Solved

Credit card expiration date

Posted on 2014-10-14
6
261 Views
Last Modified: 2016-02-17
Hello,
Credit card expiration date  - I want fill the drop down with MM/YY format

for e.g:

10/14
11/14
12/14
01/15
02/15
and so on... for next 10 or 12 years..


I am using C# /asp.net.  Any idea?


Thanks
0
Comment
Question by:mani_sai
6 Comments
 
LVL 40

Assisted Solution

by:Kyle Abrahams
Kyle Abrahams earned 200 total points
ID: 40380274
two loops should do it.  Note that this was coded by hand so there may be a couple of compiler errors.

int m = DateTime.Now.Month;

for (int y = DateTime.Now.Year; y <= DateTime.Now.Year + 12; y++)
{
  while (m <= 12)
  {
     string val = String.Format("{0:D2}/{1}", m, y.ToString().Substring(2,2));   
    //add val to dropdown list.
    m++;
  }
   m = 1;
}

Open in new window

0
 
LVL 8

Author Comment

by:mani_sai
ID: 40380281
Thanks
0
 
LVL 25

Expert Comment

by:Ron M
ID: 40380319
..why not just have two dropdowns?  ..usually it's one dropdown for months, one for years.. on most websites.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 43

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 100 total points
ID: 40380521
Here's an alternative without the need for 2 loops - just use the AddMonths() method:

DateTime expiryDate = DateTime.Today;

// Loop through for 36 months
for (int i = 0; i < 36; i++)
{
	// Add the value to your dropdown
	comboBoxExpiryDate.Items.Add((object)expiryDate.ToString("MM/yy"));
	
	// Add a month
	expiryDate = expiryDate.AddMonths(1);
}

Open in new window

0
 
LVL 33

Accepted Solution

by:
it_saige earned 200 total points
ID: 40380677
And here is an example that uses linq:
foreach (var cc in (from d in Enumerable.Range(0, 144) let now = DateTime.Now.AddMonths(d) select now.ToString("MM/yy")))
	comboBoxExpiryDate.Items.Add(cc);

Open in new window


-saige-
0
 
LVL 8

Author Comment

by:mani_sai
ID: 40382237
Thanks to all of you.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now