开发者

Format string as date

开发者 https://www.devze.com 2023-01-01 02:52 出处:网络
I have a string 20100524 (2010开发者_JAVA百科 05 24) and I would like to parse it as an actual date format.This will do it for you in a safe manner:

I have a string 20100524 (2010开发者_JAVA百科 05 24) and I would like to parse it as an actual date format.


This will do it for you in a safe manner:

DateTime dateTime;
if (DateTime.TryParseExact("20100524", "yyyyMMdd", null, DateTimeStyles.None, out dateTime))
{
    // use dateTime here
}
else
{
    // the string could not be parsed as a DateTime
}


DateTime.Parse and Datetime.ParseExact are your friends.


DateTime.ParseExact("20100524", "yyyyMMdd", Thread.CurrentThread.CurrentCulture);


DateTime result;
CultureInfo provider = CultureInfo.InvariantCulture;

string dateString = "20100524";
string format = "yyyyMMdd";
result = DateTime.ParseExact(dateString, format, provider);
0

精彩评论

暂无评论...
验证码 换一张
取 消