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

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
user429400
  • 3,145
  • 12
  • 49
  • 68
  • Doesn't DateTime.TryParse do the stuff for you, it should read this format easily I guess. Or you can simply Split date from T and still get what you need. – Sumit Gupta Oct 02 '13 at 08:28
  • 6
    "this doesn't work for me" doesn't say anything about what happens. Please be more specific. – Jon Skeet Oct 02 '13 at 08:33
  • This looks like you're not storing times as UTC. If you store times as UTC you can convert them consistently and easily to any timezone without worrying about the input format. – guymid Oct 02 '13 at 08:45
  • This article http://stackoverflow.com/questions/1536633/net-parsing-iso-8601-string-to-datetime seems to be the one you're looking for – Neil Oct 02 '13 at 08:55

4 Answers4

4

You didn't gave us any information about your error and since we don't know your culture, here and it works with 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"));

Output will be;

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

Here a DEMO.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
2

This looks like the format of XMLDateTime use XmlCovert for this purpose.

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

Ideone Demo

Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
0

Looks like you are parsing an ISO 8601 datetime string. This article Parsing ISO 8601 string to DateTime in .NET? seems to be the one you're looking for

Community
  • 1
  • 1
Neil
  • 11,059
  • 3
  • 31
  • 56
0

You can also try the below solution to get the result in dd/MMM/yyyy format in string variable:

string _dateTime=string.Empty;

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

So for the date 2013-08-24T19:29:26.4050000 will return you as 24/Aug/2013

Mohit Sahu
  • 11
  • 3