将年份从4位数转换为2位数,然后在C#中再次返回 [英] Converting a year from 4 digit to 2 digit and back again in C#
问题描述
我的信用卡处理器要求我从信用卡到期日起发送两位数的年份.这是我目前正在处理的方式:
My credit card processor requires I send a two-digit year from the credit card expiration date. Here is how I am currently processing:
- 我在页面上输入了四位数年份的
DropDownList
. - 我在
DateTime
字段中验证到期日期,以确保传递给CC处理器的到期日期没有到期. - 我将两位数字的年份发送给CC处理器(根据需要).我通过DDL年的值的子字符串来完成此操作.
- 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.
有没有一种方法可以将四位数的年份转换为两位数的年份.我没有在DateTime
对象上看到任何东西.还是我应该继续处理它?</p>
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?
推荐答案
如果要使用到期日期(月/年)创建DateTime对象,则可以在DateTime变量上使用ToString(),如下所示:>
If you're creating a DateTime object using the expiration dates (month/year), you can use ToString() on your DateTime variable like so:
DateTime expirationDate = new DateTime(2008, 1, 31); // random date
string lastTwoDigitsOfYear = expirationDate.ToString("yy");
如果在验证期间使用DateTime对象,请注意日期.如果有人选择05/2008作为卡的失效日期,则失效日期为5月底,而不是第一天.
Be careful with your dates though if you use the DateTime object during validation. If somebody selects 05/2008 as their card's expiration date, it expires at the end of May, not on the first.
这篇关于将年份从4位数转换为2位数,然后在C#中再次返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!