Solved

Credit card expiration date

Posted on 2014-10-14
6
321 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 34

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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!
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

688 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