ios NSDateFormatter "Month" 用 3 个字母代替完整的单词

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10518659/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:18:33  来源:igfitidea点击:

NSDateFormatter "Month" in 3 letters instead of full word

iosdatensdatensdateformattermonthcalendar

提问by bruno

    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"dd-MM-YYYY HH:mm"];        
    [formatter setTimeZone:[NSTimeZone systemTimeZone]];

If i choose MMi get the month in number: 09-05-2012 15:33If i choose MMMMi get the month in word: 09-May-2012 15:33

如果我选择,MM我会得到月份:09-05-2012 15:33如果我选择,MMMM我会得到月份:09-May-2012 15:33

What i wanted, was the month in 3 letters abbreviation.
Eg: Januarywould be Jan

我想要的是 3 个字母缩写的月份。
例如:JanuaryJan

In this case May is correct because it only has 3 letters.

在这种情况下 May 是正确的,因为它只有 3 个字母。

回答by graver

Have you tried: [formatter setDateFormat:@"dd-MMM-YYYY HH:mm"];and then [formatter stringFromDate:someNSDate];

你有没有试过:[formatter setDateFormat:@"dd-MMM-YYYY HH:mm"];然后[formatter stringFromDate:someNSDate];

回答by Dan Lingman

IF you get the two things in the wrong order, you don't get the expected output.

如果你以错误的顺序得到这两件事,你就不会得到预期的输出。

[formatter setDateFormat:@"MMM"];
[formatter setDateStyle:NSDateFormatterMediumStyle];

does NOT give the same results as:

不会给出与以下相同的结果:

[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setDateFormat:@"MMM"];

(The second of these two snippets is now happily handing out Sep Oct etc in my app)

(这两个片段中的第二个现在很高兴在我的应用程序中分发 Sep Oct 等)

回答by hellosheikh

In SWIFT

SWIFT

let dateFormatter = NSDateFormatter()

dateFormatter.dateFormat = "dd MMM yyyy HH:mm"

dateFormatter.stringFromDate(NSDate())

回答by CodeBender

This is a stripped down solution that works with Swift 4 in producing a 3 letter month output:

这是一个精简的解决方案,可与 Swift 4 一起生成 3 个字母的月份输出:

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd MMM YYYY, HH:mm:ss"

And to verify:

并验证:

print(dateFormatter.string(from: Date())) // 20 Mar 2018, 23:41:40

As previously noted, MMMis the correct syntax for getting the desired month output.

如前所述,MMM是获取所需月份输出的正确语法。

回答by ingconti

for swift 4 go with:

对于 swift 4,请使用:

if let dateFormatter = DateFormatter(){
    dateFormatter.dateStyle = .medium
    // eventually:
    //let locale = Locale(identifier: ...                       
    //dateFormatter!.locale = locale
    dateFormatter.dateStyle = .medium
    dateFormatter!.dateFormat = "dd MMM YYYY, HH:mm:ss"
 } else..
        ..

回答by Alladinian

Actually you can set your dateStyle to medium (ie 'Jan', 'Feb' etc) like this:

实际上,您可以将 dateStyle 设置为中等(即“Jan”、“Feb”等),如下所示:

[formatter setDateStyle:NSDateFormatterMediumStyle];

回答by Totoro

For those who do not get a full month strong with "MMM", you can get it with

对于那些没有通过“MMM”获得整整一个月强壮的人,您可以通过

dateFormatter.dateFormat = "dd MMMM yyyy"

Tested on Swift 4.2

在 Swift 4.2 上测试