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

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

问题描述

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

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天全站免登陆