如何转换格式为“YYYYMM”的字符串到“MMMYY”? [英] How can I convert a string in the format "YYYYMM" to "MMMYY"?
问题描述
我想将YYYYMM格式的值(例如201509)传递给函数,并检索人类友好的MMMYY格式,例如2015年9月。
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".
我以为这可能有效:
internal static string GetMMMYYFromYYYYMM(String YYYYMMVal)
{
string intermediateStr = YYYYMMVal + "01";
DateTime intermediateDate = Convert.ToDateTime(intermediateStr);
return intermediateDate.ToString("MMMyy");
}
...但不,它会与被认定为有效的日期时间
ISTM,YYYYMMDD格式应该可以转换并转换为DateTime。我需要改变什么?
ISTM that the YYYYMMDD format should be grokkable and convertible to a DateTime. What do I need to change?
由于我没有得到我想要的,决定蛮力,如下所示:
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);
}
...虽然它返回了我想要的9月15日 十月十五号等,我仍然在我的网页上看到15-Sep和Oct-15??
...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);
我认为Excel必须在幕后或某事上进行自动更正让我想起一个我看到的电影(Reds也许?),当他的编辑改变了他写的内容时,主角走了半场。我经常觉得这样对Word和Excel。我怎么能告诉Excel(假设这是问题),只能离开它 - 我已经写过,我写了?
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"?
推荐答案
您应该使用 DateTime.ParseExact 。所以你的代码将如下所示:
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");
}
这篇关于如何转换格式为“YYYYMM”的字符串到“MMMYY”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!