0

I am attmepting to change a date time format for a search from ddd MMM dd HH:mm:ss zzz yyyy to just simply yyyy-MM-dd i'm not able to get the syntax right in my conversion so any help would be great.

public DateTime CreatedAt { get { return DateTime.ParseExact(this.created_at, "ddd MMM dd HH:mm:ss zzz yyyy", CultureInfo.InvariantCulture); } } public DateTime SearchDate { get { return DateTime.ParseExact(CreatedAt.ToString(), "yyyy-MM-dd", CultureInfo.InvariantCulture); } } 

searchDate is my issue.

1
  • 1
    Are you getting an error, or getting a DateTime object with the wrong values? Commented Oct 15, 2013 at 20:32

1 Answer 1

2

This won't work:

return DateTime.ParseExact(CreatedAt.ToString(), "yyyy-MM-dd", CultureInfo.InvariantCulture); 

because CreatedAt.ToString() is going to create a date time string in the default format (not yyyy-MM-dd, which is how you're trying to read it).

But more importantly, why are you trying to convert a date from one string format time to another? If you have the DateTime value (CreatedAt, in this case), you can render it however you want (e.g. CreatedAt.ToString("yyyy-MM-dd")) There just doesn't seem to be any sense in converting it to a string and parsing parts of it.

And as Bob pointed out, if all you want is the date portion, take CreatedAt.Date. Try to think about what your real goal is, though and try to take the shortest route with the fewest conversions (and therefore conversion problems) as possible.

Sign up to request clarification or add additional context in comments.

1 Comment

lol yeah unfortunately some nitpicker downvoted it, even though it was was the most helpful answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.