验证一个字符串是否可以被转化为本地系统时间格式

时间:2014-02-10 16:58:17   收藏:0   阅读:281

今天有个测试需求要验证一个时间是否被显示为本地时间格式,翻了翻google,自己又做了些试验,终于找出来这么个方法。留下来以备日后查用。

 

string dateNeedToParse = "02/10/2014";
DateTime myDate = DateTime.Now;
bool compareResult = false;
compareResult = DateTime.TryParseExact(dateNeedToParse, System.Globalization.DateTimeFormatInfo.CurrentInfo.ShortDatePattern.ToString(), System.Globalization.DateTimeFormatInfo.CurrentInfo, System.Globalization.DateTimeStyles.AssumeLocal, out myDate);
Console.Write("The date " + dateNeedToParse);
if (compareResult)
{
Console.Write(" can");
}
else
{
Console.Write(" cannot");
}

Console.Write(" be parsed to local system date format!");

Console.ReadKey();

原文:http://www.cnblogs.com/Lihao2013/p/3543036.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!