Solved

Credit card expiration date

Posted on 2014-10-14
6
252 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 39

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 42

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 32

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

707 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

13 Experts available now in Live!

Get 1:1 Help Now