programing

월 int를 월 이름으로 변환

nasanasas 2020. 11. 7. 10:09
반응형

월 int를 월 이름으로 변환


나는 단순히 DateTime구조 를 사용하여 1에서 12 사이의 정수를 축약 된 월 이름으로 변환 하려고했습니다 .

내가 시도한 것은 다음과 같습니다.

DateTime getMonth = DateTime.ParseExact(Month.ToString(), 
                       "M", CultureInfo.CurrentCulture);
return getMonth.ToString("MMM");

그러나 FormatException문자열이 유효하지 않기 때문에 첫 번째 줄에 표시 DateTime됩니다. 누구든지이 작업을 수행하는 방법을 말해 줄 수 있습니까?


CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(1);

자세한 내용은 여기 를 참조하십시오.

또는

DateTime dt = DateTime.Now;
Console.WriteLine( dt.ToString( "MMMM" ) );

또는 문화 별 축약 이름 을 얻으려는 경우 .

GetAbbreviatedMonthName(1);

참고


var monthIndex = 1;
return month = DateTimeFormatInfo.CurrentInfo.GetAbbreviatedMonthName(monthIndex);

이것도 시도해 볼 수 있습니다


대신 이와 같이 할 수 있습니다.

return new DateTime(2010, Month, 1).ToString("MMM");

CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(
    Convert.ToInt32(e.Row.Cells[7].Text.Substring(3,2))).Substring(0,3) 
    + "-" 
    + Convert.ToDateTime(e.Row.Cells[7].Text).ToString("yyyy");

참고 URL : https://stackoverflow.com/questions/6286868/convert-month-int-to-month-name

반응형