如何将字符串转换为" yyyym"MMMYY"“帮助”

本文关键字:quot yyyym MMMYY 帮助 字符串 转换 | 更新日期: 2023-09-27 18:13:02

我想将YYYYMM格式的值(如"201509")传递给函数,并检索更人性化的"MMMYY"格式,如"Sep 2015"。

我想这可能行得通:

internal static string GetMMMYYFromYYYYMM(String YYYYMMVal)
{
    string intermediateStr = YYYYMMVal + "01";
    DateTime intermediateDate = Convert.ToDateTime(intermediateStr);
    return intermediateDate.ToString("MMMyy");
}

…但是没有,它崩溃了"字符串未被识别为有效的日期时间"

ISTM, YYYYMMDD格式应该是可访问的,并可转换为日期时间。我需要改变什么?

更新

由于我没有得到我想要的,我决定像这样"蛮力":

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);
}

…而且,虽然它返回我想要的"Sep 15","Oct 15"等,我仍然在我的页面上看到"15-Sep"answers"Oct-15"…?!?

我像这样调用这个助手方法:

var monthYearCell = _xlPivotDataSheet.Cells[_lastRowAddedPivotTableData + 1, 4];
monthYearCell.Value2 = ReportRunnerConstsAndUtils.GetMMMYYFromYYYYMM(MonthYear);

我估计Excel一定是在幕后"自动校正"之类的;这让我想起了我看过的一部电影(也许是《赤色》?),在这部电影中,当编辑修改了他写的东西时,主角大发雷霆。我经常对Word和Excel有这种感觉。我怎么能告诉Excel(假设这就是问题所在)不要管它——"我写的,我已经写了"?

如何将字符串转换为" yyyym"MMMYY"“帮助”

应该使用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";

try this

return DateTime.ParseExact(intermediateDate.ToString(), "yyyyMM", CultureInfo.InvariantCulture).ToString();