Solved

Credit card expiration date

Posted on 2014-10-14
6
267 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 Malmstead
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

776 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