2 Replies - 520 Views - Last Post: 23 January 2011 - 07:40 AM Rate Topic: -----

#1 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1252
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

What would be a good way to convert this date string into an actual Da

Posted 22 January 2011 - 07:31 PM

Fri Jan 21 21:39:29 GMT-0400 2011


I have the above string and I'd like to convert that string to a DateTime object. I need to at least be able to have the Date, Month, Year, Time. GMT is not really necessary.

What would be the best approach to this use-case?

Tried the following and I get an exception:

internal DateTime GetGameDate(string content)
{
    var location = content.IndexOf("creationTime");
    var creationDate = content.Substring(location + 14, 34);
    return DateTime.ParseExact(creationDate.Trim(), "yyyyMMdd HHmm", CultureInfo.InvariantCulture);
}


Quote

String was not recognized as a valid DateTime.


Before you guys ask, I triple-checked that creationDate is the string I posted above on execution time. :P

Thank you!

This post has been edited by Sergio Tapia: 22 January 2011 - 07:37 PM


Is This A Good Question/Topic? 3
  • +

Replies To: What would be a good way to convert this date string into an actual Da

#2 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 774
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

Re: What would be a good way to convert this date string into an actual Da

Posted 22 January 2011 - 09:22 PM

Your ParseExact statement is incorrect. It should be:

return DateTime.ParseExact(date, "ddd MMM dd HH:mm:ss G'M'Tzzz yyyy", CultureInfo.InvariantCulture);


Notice that M in GMT is escaped. Otherwise, it will be considered as a month indicator and an exception will be thrown since in fact there is no month value in that place.
Was This Post Helpful? 2
  • +
  • -

#3 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1252
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: What would be a good way to convert this date string into an actual Da

Posted 23 January 2011 - 07:40 AM

Thanks!

This is what I ended up with:

game.Date = DateTime.ParseExact(GetFieldContent(content, "creationTime", 33), "ddd MMM dd HH:mm:ss 'GMT'zzz yyyy", CultureInfo.InvariantCulture);

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1