问题
I want to pass values in YYYYMM format such as "201509" to a function and retrieve the human-friendlier "MMMYY" format, such as "Sep 2015".
I thought this might work:
internal static string GetMMMYYFromYYYYMM(String YYYYMMVal)
{
string intermediateStr = YYYYMMVal + "01";
DateTime intermediateDate = Convert.ToDateTime(intermediateStr);
return intermediateDate.ToString("MMMyy");
}
...but no, it crashes with "String was not recognized as a valid DateTime"
ISTM that the YYYYMMDD format should be grokkable and convertible to a DateTime. What do I need to change?
UPDATE
Since I wasn't getting quite what I wanted, I decided to "brute force it" like so:
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);
}
...and, though it returns what I want "Sep 15", "Oct 15" etc., I still see "15-Sep" and "Oct-15" on my page...?!?
I'm calling that helper method like so:
var monthYearCell = _xlPivotDataSheet.Cells[_lastRowAddedPivotTableData + 1, 4];
monthYearCell.Value2 = ReportRunnerConstsAndUtils.GetMMMYYFromYYYYMM(MonthYear);
I reckon Excel must be "autocorrecting" behind the scenes or something; reminds me of a movie I saw ("Reds" maybe?) where the protagonist went semi-ballistic when his editor changed what he wrote. I often feel this way about Word and Excel. How can I tell Excel (assuming this is the problem) to just leave it alone - "What I have written, I have written"?
回答1:
You should use DateTime.ParseExact. So your code will look like this:
internal static string GetMMMYYFromYYYYMM(String YYYYMMVal)
{
DateTime intermediateDate = DateTime.ParseExact(YYYYMMVal, "yyyyMM", CultureInfo.InvariantCulture);
return intermediateDate.ToString("MMMyy");
}
回答2:
You could use switch-case
instead of all those if-else
blocks which would, imho, increase the readability. i.e.
switch (threeCharAbbreviation)
{
case "01":
threeCharAbbreviation = "Jan";
break;
case "02":
//Etc.
default:
break;
}
And you can explicitly set format of cells in Excel. Haven't tested it myself but you can try the sample code below:
Excel.Range formatRange;
formatRange = xlWorkSheet.Range["A3", "B4"];
formatRange.NumberFormat = "MM-YY";
回答3:
try this
return DateTime.ParseExact(intermediateDate.ToString(), "yyyyMM", CultureInfo.InvariantCulture).ToString();
来源:https://stackoverflow.com/questions/40045761/how-can-i-convert-a-string-in-the-format-yyyymm-to-mmmyy