将两位数的年份转换为四位数年份 [英] Convert a two digit year to a four digit year

查看:343
本文介绍了将两位数的年份转换为四位数年份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个最佳做法的问题。我有一个实用程序以两位数的年份作为字符串,我需要将其转换为四位数的年份作为字符串。现在我做

This is a question of best practices. I have a utility that takes in a two digit year as a string and I need to convert it to a four digit year as a string. right now I do

//DOB's format is "MMM (D)D YY" that first digit of the day is not there for numbers 1-9
string tmpYear = rowIn.DOB.Substring(rowIn.DOB.Length - 3, 2); //-3 because it is 0 indexed
if (Convert.ToInt16(tmpYear) > 50)
    tmpYear = String.Format("19{0}", tmpYear);
else
    tmpYear = String.Format("20{0}", tmpYear);

我确定我做的这个可怕的错误,任何指针?

I am sure I am doing it horribly wrong, any pointers?

推荐答案

.NET框架有一个完全符合你想要的方法:

The .NET framework has a method that does exactly what you want:

int fourDigitYear = CultureInfo.CurrentCulture.Calendar.ToFourDigitYear(twoDigitYear)

这样你将正确遵守控制面板(或组策略)中定义的当前区域设置:

That way you will correctly adhere to current regional settings as defined in Control Panel (or group policy):

这篇关于将两位数的年份转换为四位数年份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆