如何将格式为 "YYYYMM" 的字符串转换为 "MMMYY"?
How can I convert a string in the format "YYYYMM" to "MMMYY"?
我想将 YYYYMM 格式的值(例如“201509”)传递给函数并检索更人性化的 "MMMYY" 格式,例如 "Sep 2015".
我认为这可能有效:
internal static string GetMMMYYFromYYYYMM(String YYYYMMVal)
{
string intermediateStr = YYYYMMVal + "01";
DateTime intermediateDate = Convert.ToDateTime(intermediateStr);
return intermediateDate.ToString("MMMyy");
}
...但是不,它崩溃并显示“字符串未被识别为有效的 DateTime”
ISTM 认为 YYYYMMDD 格式应该是可理解的并且可以转换为 DateTime。我需要更改什么?
更新
因为我没有得到我想要的,所以我决定 "brute force it" 像这样:
internal static string GetMMMYYFromYYYYMM(String YYYYMMVal)
{
string yearAsTwoChars = YYYYMMVal.Substring(2, 2);
string threeCharAbbreviation = YYYYMMVal.Substring(4, 2);
if (threeCharAbbreviation.Equals("01"))
{
threeCharAbbreviation = "Jan";
}
else if (threeCharAbbreviation.Equals("02"))
{
threeCharAbbreviation = "Feb";
}
else if (threeCharAbbreviation.Equals("03"))
{
threeCharAbbreviation = "Mar";
}
else if (threeCharAbbreviation.Equals("04"))
{
threeCharAbbreviation = "Apr";
}
else if (threeCharAbbreviation.Equals("05"))
{
threeCharAbbreviation = "May";
}
else if (threeCharAbbreviation.Equals("06"))
{
threeCharAbbreviation = "Jun";
}
else if (threeCharAbbreviation.Equals("07"))
{
threeCharAbbreviation = "Jul";
}
else if (threeCharAbbreviation.Equals("08"))
{
threeCharAbbreviation = "Aug";
}
else if (threeCharAbbreviation.Equals("09"))
{
threeCharAbbreviation = "Sep";
}
else if (threeCharAbbreviation.Equals("10"))
{
threeCharAbbreviation = "Oct";
}
else if (threeCharAbbreviation.Equals("11"))
{
threeCharAbbreviation = "Nov";
}
else if (threeCharAbbreviation.Equals("12"))
{
threeCharAbbreviation = "Dec";
}
return string.Format("{0} {1}", threeCharAbbreviation, yearAsTwoChars);
}
...而且,虽然它 returns 我想要 "Sep 15"、"Oct 15" 等,但我仍然在我的屏幕上看到“15-Sep”和 "Oct-15"页...?!?
我是这样调用辅助方法的:
var monthYearCell = _xlPivotDataSheet.Cells[_lastRowAddedPivotTableData + 1, 4];
monthYearCell.Value2 = ReportRunnerConstsAndUtils.GetMMMYYFromYYYYMM(MonthYear);
我估计Excel一定是"autocorrecting"在幕后之类的;让我想起了我看过的一部电影("Reds" 也许吧?)当他的编辑改变他写的内容时,主角变得半弹道。我经常对 Word 和 Excel 有这种感觉。我怎么能告诉 Excel(假设这是问题所在)不要管它 - "What I have written, I have written"?
试试这个
return DateTime.ParseExact(intermediateDate.ToString(), "yyyyMM", CultureInfo.InvariantCulture).ToString();
你应该使用 DateTime.ParseExact。所以您的代码将如下所示:
internal static string GetMMMYYFromYYYYMM(String YYYYMMVal)
{
DateTime intermediateDate = DateTime.ParseExact(YYYYMMVal, "yyyyMM", CultureInfo.InvariantCulture);
return intermediateDate.ToString("MMMyy");
}
您可以使用 switch-case
而不是所有那些 if-else
块,恕我直言,这会增加可读性。即
switch (threeCharAbbreviation)
{
case "01":
threeCharAbbreviation = "Jan";
break;
case "02":
//Etc.
default:
break;
}
并且您可以在 Excel 中明确设置单元格的格式。我自己还没有测试过,但您可以尝试下面的示例代码:
Excel.Range formatRange;
formatRange = xlWorkSheet.Range["A3", "B4"];
formatRange.NumberFormat = "MM-YY";
我想将 YYYYMM 格式的值(例如“201509”)传递给函数并检索更人性化的 "MMMYY" 格式,例如 "Sep 2015".
我认为这可能有效:
internal static string GetMMMYYFromYYYYMM(String YYYYMMVal)
{
string intermediateStr = YYYYMMVal + "01";
DateTime intermediateDate = Convert.ToDateTime(intermediateStr);
return intermediateDate.ToString("MMMyy");
}
...但是不,它崩溃并显示“字符串未被识别为有效的 DateTime”
ISTM 认为 YYYYMMDD 格式应该是可理解的并且可以转换为 DateTime。我需要更改什么?
更新
因为我没有得到我想要的,所以我决定 "brute force it" 像这样:
internal static string GetMMMYYFromYYYYMM(String YYYYMMVal)
{
string yearAsTwoChars = YYYYMMVal.Substring(2, 2);
string threeCharAbbreviation = YYYYMMVal.Substring(4, 2);
if (threeCharAbbreviation.Equals("01"))
{
threeCharAbbreviation = "Jan";
}
else if (threeCharAbbreviation.Equals("02"))
{
threeCharAbbreviation = "Feb";
}
else if (threeCharAbbreviation.Equals("03"))
{
threeCharAbbreviation = "Mar";
}
else if (threeCharAbbreviation.Equals("04"))
{
threeCharAbbreviation = "Apr";
}
else if (threeCharAbbreviation.Equals("05"))
{
threeCharAbbreviation = "May";
}
else if (threeCharAbbreviation.Equals("06"))
{
threeCharAbbreviation = "Jun";
}
else if (threeCharAbbreviation.Equals("07"))
{
threeCharAbbreviation = "Jul";
}
else if (threeCharAbbreviation.Equals("08"))
{
threeCharAbbreviation = "Aug";
}
else if (threeCharAbbreviation.Equals("09"))
{
threeCharAbbreviation = "Sep";
}
else if (threeCharAbbreviation.Equals("10"))
{
threeCharAbbreviation = "Oct";
}
else if (threeCharAbbreviation.Equals("11"))
{
threeCharAbbreviation = "Nov";
}
else if (threeCharAbbreviation.Equals("12"))
{
threeCharAbbreviation = "Dec";
}
return string.Format("{0} {1}", threeCharAbbreviation, yearAsTwoChars);
}
...而且,虽然它 returns 我想要 "Sep 15"、"Oct 15" 等,但我仍然在我的屏幕上看到“15-Sep”和 "Oct-15"页...?!?
我是这样调用辅助方法的:
var monthYearCell = _xlPivotDataSheet.Cells[_lastRowAddedPivotTableData + 1, 4];
monthYearCell.Value2 = ReportRunnerConstsAndUtils.GetMMMYYFromYYYYMM(MonthYear);
我估计Excel一定是"autocorrecting"在幕后之类的;让我想起了我看过的一部电影("Reds" 也许吧?)当他的编辑改变他写的内容时,主角变得半弹道。我经常对 Word 和 Excel 有这种感觉。我怎么能告诉 Excel(假设这是问题所在)不要管它 - "What I have written, I have written"?
试试这个
return DateTime.ParseExact(intermediateDate.ToString(), "yyyyMM", CultureInfo.InvariantCulture).ToString();
你应该使用 DateTime.ParseExact。所以您的代码将如下所示:
internal static string GetMMMYYFromYYYYMM(String YYYYMMVal)
{
DateTime intermediateDate = DateTime.ParseExact(YYYYMMVal, "yyyyMM", CultureInfo.InvariantCulture);
return intermediateDate.ToString("MMMyy");
}
您可以使用 switch-case
而不是所有那些 if-else
块,恕我直言,这会增加可读性。即
switch (threeCharAbbreviation)
{
case "01":
threeCharAbbreviation = "Jan";
break;
case "02":
//Etc.
default:
break;
}
并且您可以在 Excel 中明确设置单元格的格式。我自己还没有测试过,但您可以尝试下面的示例代码:
Excel.Range formatRange;
formatRange = xlWorkSheet.Range["A3", "B4"];
formatRange.NumberFormat = "MM-YY";