Help translating something from coldfusion to c#

dalearyous

Senior member
Jan 8, 2006
836
0
0
Code:
       <cfset period = DateFormat(CreateDate(currentYear, currentMonth, 1), "yy-mm")>

this should return period to 12-01 ... its the current year and month prior to the current month.

i could so something like this and combine them but figured there was better way.

Code:
        int month = DateTime.Today.Month;
        int year = DateTime.Today.Year;

and this way would not return 2 digits for single digit months.
 
Last edited:

Crusty

Lifer
Sep 30, 2001
12,684
2
81
Code:
string period = string.Format("{0}-{1}", DateTime.Today.AddMonths(-1).Month, DateTime.Today.AddYears(-1).Year);

You can just adjust the format string to make it pad 0's if you want, I forget the exact syntax but it is well documented on MSDN.
 
Last edited:

dalearyous

Senior member
Jan 8, 2006
836
0
0
lol yeah i just realized that

the year needs to be 2 numbers ... so 2012 needs to be 12

hah, ghetto way:
Code:
DateTime.Today.AddYears(-2000).Year);

*edit*
Code:
string period = DateTime.Now.AddMonths(-1).ToString("MM-yy");

THERE!
 
Last edited:

Crusty

Lifer
Sep 30, 2001
12,684
2
81
lol yeah i just realized that

the year needs to be 2 numbers ... so 2012 needs to be 12

hah, ghetto way:
Code:
DateTime.Today.AddYears(-2000).Year);

*edit*
Code:
string period = DateTime.Now.AddMonths(-1).ToString("MM-yy");

THERE!

Oh, I just realized I misread your OP. You just wanted the previous month, your way is definitely better for that.