3

Possible Duplicate:
How to convert DateTime object to dd/mm/yyyy in C#?

I'm new to c# and was hoping someone could help me clean up some code.

I have the following method which converts a DateTime to a custom Event string e.g. 30th of Jan 2012 is converted to 201201 (ignores the day)

public ConvertToEventDate(DateTime date)
{   
    var year = date.Year.ToString();
    var month = date.Month.ToString();
    month = month.Length == 2 ? month : "0" + month;
    return year + month;
}

I was wondering if there is a better way of doing this conversion.

Community
  • 1
  • 1
  • The above will not compile - there is no return type defined in the function signature. – Oded Aug 24 '12 at 11:34

4 Answers4

2
public string ConvertToEventDate(DateTime date)
{ 
    return date.ToString("yyyyMM", CultureInfo.InvariantCulture);
} 

Have a look at the custom formatting strings docs

Oded
  • 489,969
  • 99
  • 883
  • 1,009
Dan
  • 45,079
  • 17
  • 88
  • 157
  • I don't like the edit above, I don't see the point of putting this in a function. simply MyDateTimeObject.ToString("yyyyMM") is easier to read, encapsulating this in a new function adds nothing imo. – Dan Aug 24 '12 at 11:36
  • It is in a function because the OP put it in a function. – Oded Aug 24 '12 at 11:39
  • The OP asked for cleaner code. I think it is cleaner and clearer to call date.ToString("yyyyMM") than to call ConvertToEventDate(date). – Dan Aug 24 '12 at 11:40
  • Nothing stops you from editing back. But at least conserve the _correct_ casing. – Oded Aug 24 '12 at 11:43
  • Thinking about it, I guess putting it in a function allows the event date format to be changed more easily in the future. So I see where you are coming from. – Dan Aug 24 '12 at 11:47
2

I'd do this;

public string ConvertToEventDate(DateTime date)
{   
    return date.ToString("yyyyMM");
}

you can also put this into an Extension method like this;

public static class ExtenstionMethods
{
    public static string ToEventDate(this DateTime date)
    {
        return date.ToString("yyyyMM");
    }
}

and then call it ike this;

DateTime date = new DateTime(2012, 30, 1);

    date.ToEventDate();

as opposed to this;

ConvertToEventDate(date);
saj
  • 4,626
  • 2
  • 26
  • 25
  • I like the extension method idea, because it's being called from all over the code so prefer to keep it centralised – user1622452 Aug 24 '12 at 11:58
2

Others have suggested ToString("yyyyMM") which is basically there - but I would suggest you probably want to specify the invariant culture. Otherwise if the thread's current culture uses a non-Gregorian calendar, you could end up with a month/year you're not expecting. So I'd use:

string text = date.ToString("yyyyMM", CultureInfo.InvariantCulture);

See the documentation for custom date and time format strings for more information if you want to change the exact format later.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

Try

return date.ToString("yyyyMM");
tomsv
  • 7,207
  • 6
  • 55
  • 88