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

今天有个测试需求要验证一个时间是否被显示为本地时间格式,翻了翻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();

posted on 2014-02-10 15:50  小昊子  阅读(169)  评论(0编辑  收藏  举报

导航