Programming Newbie
Programming Newbie

Reputation: 1227

DateTime Format yyyymm

I've found every DateTime format you can think of except the one I need: yyyymm. How do I go about getting this?

I have the following code I'm working with:

List <string> routines = new List<string>();
routines.Add("1 - Routine");
routines.Add("6 - Planned Outage");

int year = 2011;
int quarter = 1;

DateTime timeframe = new DateTime(year, (quarter * 3), 01).AddMonths(1); 

var results =

    (from wo in WORKORDERs

        join wot in WORKORDERTYPEs on wo.Wot_oi equals wot.Wotyoi

        join pri in PRIORITies on wo.Prio_oi equals pri.Priooi

        join s in SITEs on wo.BEparn_oi equals s.Siteoi

    where wo.Audt_created_dttm.Value.Year >= year - 3 && wo.Audt_created_dttm.Value.Year >= 2006
        && wo.Audt_created_dttm < timeframe && (s.Id =="NM" || s.Id == "TH") && 
        !wot.Id.Contains("stand") &&  !(wo.Ci_cnc_date != null && pri.Prioid != "1 - Routine" &&
        pri.Prioid != "6 - Planned Outage") && (SqlMethods.Like(pri.Prioid, "1%") || 
        SqlMethods.Like(pri.Prioid, "2%") || SqlMethods.Like(pri.Prioid, "3%"))     

    select new {PM = wo.Wosource, Site = s.Id, Priority = pri.Prioid, Worktype = wot.Id,
        WoNumber = wo.Id, Description = wo.Aenm, CreateDate = wo.Audt_created_dttm,
        CloseDate = wo.Clsdt_date,
        MonthNum = String.Format("{0:yyyy mm}",  wo.Clsdt_date), 
        Planning = routines.Contains(pri.Prioid) ? "Planned" : "Unplanned"});

I tried using:

MonthNum = String.Format("{0:yyyy mm}",  wo.Clsdt_date) 

and that gives me the year correctly but the months are all 00. I'm guessing this is because the format for wo.Clsdt_date is different than my string format (it's mm/dd/yyyy).

Any ideas?

Upvotes: 3

Views: 11185

Answers (3)

Vlad Bezden
Vlad Bezden

Reputation: 89499

"mm" is for minutes, "MM" is for Month, but with 2 digits of month. So in June 2012 you will have 201206, however if you need only one digit for month, you can use one "M" instead "yyyyM" and you will get 20126

DateTime.Now.ToString("yyyyMM")

Upvotes: 4

Adam V
Adam V

Reputation: 6356

In date formatting, mm means minutes, not months. Use MM instead.

Upvotes: 1

Rawling
Rawling

Reputation: 50104

You need MM, not mm. mm is minutes.

Upvotes: 15

Related Questions