Reputation: 427
I've just started using C#. I'm trying to create a static method that takes a string as such; 2018-03-15T08:50:00-05:00
and converts it to Mar 3, 2018
. I've researched this for a long time but none of the questions seem to convert to this format, rather all of the answers convert to dd-mm-yyyy
.
I think it would be something like this:
public static string ToProperDate(this string input)
{
return String.Format("{0:dddd, MMMM d, yyyy}", input);
}
Could someone please help me? Thank-you so much.
Upvotes: 0
Views: 427
Reputation: 157048
You should parse the text to a DateTime
first to ease formatting it.
So change the input to a DateTime
instance:
public static string ToProperDate(this DateTime input)
{
return String.Format("{0:dddd, MMMM d, yyyy}", input);
}
Or parse it in your method:
public static string ToProperDate(this string input)
{
DateTime d = DateTime.Parse(input);
return String.Format("{0:dddd, MMMM d, yyyy}", d);
}
Upvotes: 5