将年份从 4 位数字转换为 2 位数字并在 C# 中再次返回
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/115399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting a year from 4 digit to 2 digit and back again in C#
提问by Mike Wills
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.
- 我
DropDownList
在页面上放了一个4 位数年份。 - 我在一个
DateTime
字段中验证到期日期,以确保传递给 CC 处理器的到期日期没有到期。 - 我将两位数的年份发送到 CC 处理器(根据需要)。我通过年份 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?
有没有一种方法可以将四位数年份转换为两位数年份。我没有在物体上看到任何东西DateTime
。还是我应该继续按原样处理它?
采纳答案by brock.holum
If you're creating a DateTime object using the expiration dates (month/year), you can use ToString() on your DateTime variable like so:
如果您使用到期日期(月/年)创建 DateTime 对象,则可以在 DateTime 变量上使用 ToString() ,如下所示:
DateTime expirationDate = new DateTime(2008, 1, 31); // random date
string lastTwoDigitsOfYear = expirationDate.ToString("yy");
Edit: 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.
编辑:如果您在验证期间使用 DateTime 对象,请注意您的日期。如果有人选择 05/2008 作为他们卡的到期日期,它会在 5 月底而不是 5 月底到期。
回答by NotMe
At this point, the simplest way is to just truncate the last two digits of the year. For credit cards, having a date in the past is unnecessary so Y2K has no meaning. The same applies for if somehow your code is still running in 90+ years.
此时,最简单的方法是截断年份的最后两位数字。对于信用卡,过去的日期是不必要的,因此 Y2K 没有意义。如果您的代码在 90 多年后仍在运行,这同样适用。
I'd go further and say that instead of using a drop down list, let the user type in the year themselves. This is a common way of doing it and most users can handle it.
我会更进一步说,不要使用下拉列表,而是让用户自己输入年份。这是一种常见的方法,大多数用户都可以处理。
回答by Andrew Jahn
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.
为什么不让页面上的原始下拉列表仅为 2 位数?信用卡在查看年份时只涵盖很小的跨度,特别是如果 CC 供应商已经只接受 2 位数。
回答by Vinko Vrsalovic
Even if a builtin way existed, it wouldn't validate it as greater than today and it would differ very little from a substring call. I wouldn't worry about it.
即使存在内置方式,它也不会验证它比今天更大,并且它与子字符串调用几乎没有区别。我不会担心的。
回答by ckramer
Use the DateTime object ToString with a custom format string like myDate.ToString("MM/dd/yy") for example.
例如,将 DateTime 对象 ToString 与自定义格式字符串一起使用,例如 myDate.ToString("MM/dd/yy")。
回答by spoulson
I've seen some systems decide that the cutoff is 75; 75+ is 19xx and below is 20xx.
我见过一些系统决定截止值为 75;75+ 是 19xx,低于 20xx。
回答by Seb Nilsson
This should work for you:
这应该适合你:
public int Get4LetterYear(int twoLetterYear)
{
int firstTwoDigits =
Convert.ToInt32(DateTime.Now.Year.ToString().Substring(2, 2));
return Get4LetterYear(twoLetterYear, firstTwoDigits);
}
public int Get4LetterYear(int twoLetterYear, int firstTwoDigits)
{
return Convert.ToInt32(firstTwoDigits.ToString() + twoLetterYear.ToString());
}
public int Get2LetterYear(int fourLetterYear)
{
return Convert.ToInt32(fourLetterYear.ToString().Substring(2, 2));
}
I don't think there are any special built-in stuff in .NET.
我认为 .NET 中没有任何特殊的内置内容。
Update: It's missing some validation that you maybe should do. Validate length of inputted variables, and so on.
更新:它缺少一些您可能应该做的验证。验证输入变量的长度等。
回答by Harv
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.
这是 4Guys 文章的链接,该文章介绍了如何使用 ToString() 方法通过传入自定义格式字符串来格式化日期和时间。
http://www.aspfaqs.com/aspfaqs/ShowFAQ.asp?FAQID=181
http://www.aspfaqs.com/aspfaqs/ShowFAQ.asp?FAQID=181
Just in case it goes away here is one of the examples.
以防万一它消失这里是例子之一。
'Create a var. named rightNow and set it to the current date/time
Dim rightNow as DateTime = DateTime.Now
Dim s as String 'create a string
s = rightNow.ToString("MMM dd, yyyy")
Since his link is broken here is a link to the DateTimeFormatInfo class that makes those formatting options possible.
由于他的链接在此处断开,因此这里有一个指向 DateTimeFormatInfo 类的链接,该类使这些格式选项成为可能。
http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo.aspx
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.
这样做可能比使用子字符串更一致,但谁知道呢。
回答by V.K.D Baghel
//using java script
var curDate = new Date();
var curYear = curDate.getFullYear();
curYear = curYear.toString().slice(2);
document.write(curYear)
//using java script
//using sqlserver
select Right(Year(getDate()),2)
//using sql server
//Using c#.net
DateTime dt = DateTime.Now;
string curYear = dt.Year.ToString().Substring(2,2).ToString() ;
//using c#.net
回答by Phiplex
DateTime.Now.Year - (DateTime.Now.Year / 100 * 100)
Works for current year. Change DateTime.Now.Year
to make it work also for another year.
为当年工作。更改DateTime.Now.Year
以使其也工作一年。