0

Can't seem to get this one right. What format string should I use for the following DateTime input ?

2013-08-24T19:29:26.4050000

I want to extract the date.

I tried:

DateTime.ParseExact(localTime, "yyyy-MM-ddTHH:mm:ss.fffffff", null).ToString("yyyy-MM-dd");

But this doesn't work for me, plus I feel there must be a way to avoid the multiple "f" at the end (I just need the date, don't care about the hour)

Thanks, Li

4

4 回答 4

4

您没有向我们提供有关您的错误的任何信息,并且由于我们不了解您的文化,因此在这里它适用于InvariantCulture;

string localTime = "2013-08-24T19:29:26.4050000";
DateTime date = DateTime.ParseExact(localTime, "yyyy-MM-ddTHH:mm:ss.fffffff", CultureInfo.InvariantCulture);
Console.WriteLine(date);
Console.WriteLine(date.ToString("yyyy-MM-dd"));

输出将是;

8/24/2013 7:29:26 PM
2013-08-24

这里一个DEMO.

于 2013-10-02T08:30:02.050 回答
2

这看起来像用于此目的的XMLDateTime格式XmlCovert

string fmt = "2013-08-24T19:29:26.4050000";
var dt = XmlConvert.ToDateTime(fmt, XmlDateTimeSerializationMode.Unspecified);
Console.WriteLine(dt);

Ideone 演示

于 2013-10-02T08:41:07.807 回答
0

看起来您正在解析 ISO 8601 日期时间字符串。这篇文章在 .NET 中将 ISO 8601 字符串解析为 DateTime?似乎是你要找的那个

于 2013-10-02T08:55:52.500 回答
0

您还可以尝试以下解决方案,以字符串变量中的 dd/MMM/yyyy 格式获取结果:

string _dateTime=string.Empty;

_dateTime=localTime.ToString(dd/MMM/yyyy);

因此,对于日期 2013-08-24T19:29:26.4050000 将返回您为 24/Aug/2013

于 2013-10-02T09:07:28.513 回答