Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

ok.. i have a drop down list that is populated by the following code

// Set the exp year drop down list values.
int currentYear = DateTime.Now.Year;
int finalYear = currentYear + 5;

ddlExpYear.Items.Add(new ListItem("", ""));

for(int i=currentYear; i<finalYear; i++)
    ddlExpYear.Items.Add(new ListItem(i.ToString(), i.ToString()));

 

works fine but it gives me the values 2005-2010

 

How can I tweak this to get it to show 05-10. I basically need to get a month value out of one ddl and then the year value in the other ddl and be able to assign them to an int in like this '1206'.

 

Any suggestions?

Thanks

  • *Experts*
Posted

Assuming your years are always > 2000, just subtract 2000:

// Set the exp year drop down list values.
int currentYear = DateTime.Now.Year - 2000;
int finalYear = (currentYear + 6);

for(int i=currentYear; i<finalYear; i++)
Debug.WriteLine(i.ToString("00"));

 

Have you considered storing the full year in the dropdown and just converting to a 2 digit year when they pull the value out?

 

-ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

nope.. hadn't thought about changing it after the value is selected... i think the -2000 should work, but if you feel like it, could you post an example for after the selection?

 

 

***EDIT

hmm.. subtracting 2000 seems to give me the following

5,6,7,8,9

 

I'm trying to get 05,06,07 etc.. man I can't wait till i get better at this stuff :)

  • *Experts*
Posted

Note the "00" on the ToString() method call - it will pad the number.

 

You can add the year value as a 4 digit year (as a string). When you pull out the string ("2002" for example), which you know is a number, you can then convert it to a 2 digit year however you want. You might still want to subtract 2000 - or some other "smarter" function, if needed.

 

-ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

man.. still having problems.. .here's the overall picture. I have to drop down lists ddlMonth and ddlYear. ddlMonth is populated like this.

 

<asp:ListItem Value="1">01</asp:ListItem>
<asp:ListItem Value="2">02</asp:ListItem>
<asp:ListItem Value="3">03</asp:ListItem>
...
<asp:ListItem Value="12">12</asp:ListItem>

 

the ddlYear is populated like this.

// Set the exp year drop down list values.
int currentYear = DateTime.Now.Year - 2000;
int finalYear = currentYear + 6;

ddlExpYear.Items.Add(new ListItem("", ""));
for(int i=currentYear; i<finalYear; i++)
    ddlExpYear.Items.Add(new ListItem(i.ToString("00"), i.ToString("00")));

 

What I'm trying to do is to get an int variable that ends up in this format '0205'... I can't seem to do it... I get '205'

 

string month = ddlExpMonth.SelectedValue.Trim();
string year = ddlExpYear.SelectedValue.Trim();

int ExpDate = Int32.Parse(month+year);

 

Please help if possible.

Posted
ok.. but i need to send an integer value to an api... it's expecting the format to be a four digit integer such as '0105' or '0607' or whatever... how do i get it in that format? any suggestions?
Posted

sure.. when i sent 105 i got an error.. this is cut and pasted from the integration guide.. .thanks so much for your patience and help

 

 

EXPDATE -- Expiration date of the credit card in mmyy format.

For example, 0308 represents March 2008.

 

Required=Yes

 

Type=Numeric

 

MaxLength=4

  • Leaders
Posted
Just a quick side note: I recommend using (Year) Mod 100 instead of (Year) - 2000 to get the last two digits of a year... otherwise, 1998 - 2000 will return a year of -2 instead of 98. I guess this would be Nerseus' "smarter" function.
[sIGPIC]e[/sIGPIC]
  • *Experts*
Posted

The "smarter" function I was thinking of is the one used by SQL Server, which is usually a 50/50 split or, more commonly, 80/20. Meaning, 80-99 is 1900 + value, values 00-79 are 2000 + value.

 

In the case of an expiration date, I think you can safely assume that every value is 2000+, that's why I wouldn't currently worry about 1900-1999 dates - meaning, subtract 2000 and you should be fine until 3000.

 

I agree with Plausibly, let's see that API declaration to help further. I would bet that the API wants a string. In that case, simply use something like:

string expiration = ddlExpMonth.SelectedValue.Trim() + ddlExpYear.SelectedValue.Trim();

 

That assumes you're storing a 2 digit month and year in each dropdown. Otherwise, get the month and year as an int and use something like the following to piece it together:

int month = 11; // get real value from dropdown
int year = 2008; // get real value from dropdown
string expiration = month.ToString("00") + (year - 2000).ToString("00");

 

-ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...