My credit card processor requires I send a two-digit year from the credit card expiration date. Here is how I am currently processing:
- I put a
DropDownList
of the 4-digit year on the page. - I validate the expiration date in a
DateTime
field to be sure that the expiration date being passed to the CC processor isn't expired. - I send a two-digit year to the CC processor (as required). I do this via a substring of the value from the year DDL.
Is there a method out there to convert a four-digit year to a two-digit year. I am not seeing anything on the DateTime
object. Or should I just keep processing it as I am?
The answer is quite simple:
DateTime Today = DateTime.Today; string zeroBased = Today.ToString("yy-MM-dd");
I've seen some systems decide that the cutoff is 75; 75+ is 19xx and below is 20xx.
Why not have the original drop down on the page be a 2 digit value only? Credit cards only cover a small span when looking at the year especially if the CC vendor only takes in 2 digits already.
Here is a link to a 4Guys article on how you can format Dates and Times using the ToString() method by passing in a custom format string.
http://www.aspfaqs.com/aspfaqs/ShowFAQ.asp?FAQID=181
Just in case it goes away here is one of the examples.
Since his link is broken here is a link to the DateTimeFormatInfo class that makes those formatting options possible.
http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo.aspx
It's probably a little more consistent to do something like that rather than use a substring, but who knows.
Works for current year. Change
DateTime.Now.Year
to make it work also for another year.This should work for you:
I don't think there are any special built-in stuff in .NET.
Update: It's missing some validation that you maybe should do. Validate length of inputted variables, and so on.