iOS:如何从数字中获取正确的月份名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4488373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS: How to get a proper Month name from a number?
提问by Sjakelien
I know that the NSDateformatter suite of functionality is a boon for mankind, but at the same time it is very confusing to me. I hope you can help me out.
我知道 NSDateformatter 功能套件对人类来说是一个福音,但同时它也让我感到非常困惑。我希望你能帮助我。
Somewhere in my code, there is an int representing a month. So: 1 would be January, 2 February, etc.
在我的代码中,有一个 int 代表一个月。所以:1 是 1 月,2 月 2 日,等等。
In my user interface, I would like to display this integer as proper month name. Moreover, it should adhere to the locale of the device.
在我的用户界面中,我想将此整数显示为正确的月份名称。此外,它应该符合设备的区域设置。
Thank you for your insights
谢谢你的见解
In the mean time, I have done the following:
同时,我做了以下工作:
int monthNumber = 11
NSString * dateString = [NSString stringWithFormat: @"%d", monthNumber];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM"];
NSDate* myDate = [dateFormatter dateFromString:dateString];
[dateFormatter release];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MMMM"];
NSString *stringFromDate = [formatter stringFromDate:myDate];
[formatter release];
is this the way to do it? It seems a bit wordy.
这是这样做的方法吗?好像有点啰嗦。
回答by
Another option is to use the monthSymbols method:
另一种选择是使用 monthSymbols 方法:
int monthNumber = 11; //November
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
NSString *monthName = [[df monthSymbols] objectAtIndex:(monthNumber-1)];
Note that you'll need to subtract 1 from your 1..12 monthNumber since monthSymbols is zero-based.
请注意,您需要从 1..12 monthNumber 中减去 1,因为 monthSymbols 是从零开始的。
回答by quemeful
Swift 2.0
斯威夫特 2.0
let monthName = NSDateFormatter().monthSymbols[monthNumber - 1]
Swift 4.0
斯威夫特 4.0
let monthName = DateFormatter().monthSymbols[monthNumber - 1]
回答by Carl
You can change the dateFormat of the NSDateFormatter. So to simplify your code:
您可以更改 NSDateFormatter 的 dateFormat。所以为了简化你的代码:
int monthNumber = 11
NSString * dateString = [NSString stringWithFormat: @"%d", monthNumber];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM"];
NSDate* myDate = [dateFormatter dateFromString:dateString];
[formatter setDateFormat:@"MMMM"];
NSString *stringFromDate = [formatter stringFromDate:myDate];
[dateFormatter release];
You should also set the locale once you init the date formatter.
初始化日期格式化程序后,您还应该设置语言环境。
dateFormatter.locale = [NSLocale currentLocale]; // Or any other locale
Hope this helps
希望这可以帮助
回答by Nico
Best solution for this is , standaloneMonthSymbols
method,
对此的最佳解决方案是,standaloneMonthSymbols
方法,
-(NSString*)MonthNameString:(int)monthNumber
{
NSDateFormatter *formate = [NSDateFormatter new];
NSArray *monthNames = [formate standaloneMonthSymbols];
NSString *monthName = [monthNames objectAtIndex:(monthNumber - 1)];
return monthName;
}
回答by Pascal
How about:
怎么样:
NSUInteger i = <your month integer>;
NSDateFormatter *df = [NSDateFormatter new];
// change locale if the standard is not what you want
NSArray *monthNames = [df standaloneMonthSymbols];
NSString *monthName = [monthNames objectAtIndex:(i - 1)];
[df release];
回答by serb
Both answers from Anna Karenina and Carl doesn't work that well as they won't return month name in nominativ for some cultures. I suggest to use the proposed solution from Pascal, which solves this issue (by replacing monthSymbols
with standaloneMonthSymbols
)
Anna Karenina 和 Carl 的两个答案都没有那么好用,因为他们不会在某些文化的名称中返回月份名称。我建议使用 Pascal 提出的解决方案,它解决了这个问题(通过替换monthSymbols
为standaloneMonthSymbols
)
回答by Heshan Sandeepa
In Swift 3.0
在 Swift 3.0 中
let monthNumber = 3
let fmt = DateFormatter()
fmt.dateFormat = "MM"
let month = fmt.monthSymbols[monthNumber - 1]
print(month)
// result
"March\n"
回答by Ashu
Swift 4.X
斯威夫特 4.X
print((DateFormatter().monthSymbols[month-1].capitalized)) //month is int less than 12
print((DateFormatter().monthSymbols[month-1].capitalized)) //month is int less than 12
For Example:
例如:
print((DateFormatter().monthSymbols[11-1].capitalized))
print((DateFormatter().monthSymbols[11-1].capitalized))
Output
输出
November
十一月
回答by PradeepKN
NSDate to NSString-> As Dateformat Ex:2015/06/24
NSDate 到 NSString -> As Dateformat 例如:2015/06/24
NSDateFormatter *dateformate=[[NSDateFormatter alloc]init];
[dateformate setDateFormat: @"yyyy/MM/dd"];
NSString *date = [dateformate stringFromDate:selectedDate]; // Convert date to string
NSDate to NSString-> As Dateformat Ex:2015 June 24, 1:02 PM
NSDate 到 NSString -> As Dateformat 例如:2015 年 6 月 24 日下午 1:02
[dateformate setDateFormat:@"yyyy MMMM dd, h:mm a"];
NSString *displayDate = [dateformate stringFromDate:selectedDate]; // Convert date to string
NSLog(@"date :%@",date);
NSLog(@"Display time = %@", displayDate);
回答by Kevin Delord
And with ARC :
并使用 ARC :
+ (NSString *)monthNameFromDate:(NSDate *)date {
if (!date) return @"n/a";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"MM"];
return [[df monthSymbols] objectAtIndex:([[df stringFromDate:date] integerValue] - 1)];
}