NSDate完整风格但没有年份

Mar*_*tin 30 iphone nsdate nsdateformatter

我正在使用Objective C for iPhone并拥有一个NSDate,我希望以完整的风格显示但没有年份.现在我正在使用下面的代码,但它也显示了年份,我不希望这样.由于我想以正确的地区格式显示日期,我不能只删除最后一年,因为在某些国家,年份不会在最后,而是在中间或开始.

NSDate *testdate = [NSDate date];
NSDateFormatter *dateFormattertest = [[NSDateFormatter alloc] init];
[dateFormattertest setDateStyle:NSDateFormatterFullStyle];
[dateFormattertest setTimeStyle:NSDateFormatterNoStyle];
NSString *formattedDateString = [dateFormattertest stringFromDate:testdate];
NSLog(formattedDateString);

In US this give me:
Thursday, January 14, 2010

In Sweden this give me:
torsdag, 2010 januari 14
Run Code Online (Sandbox Code Playgroud)

这是什么解决方案?另一个问题,我在哪里可以找到有关如何使用NSDateFormatter的setDateFormat的信息?我无法找到有关API中不同字母的信息.

Jos*_*oss 35

我知道这是一个非常古老的问题,但这是我在SO上发现的唯一一个谈论我遇到的同样问题的问题.

这里使用的关键方法是:dateFormatFromTemplate

您需要将格式设置为:而不是设置样式:

NSDate *testdate = [NSDate date];

NSLocale *currentLocale = [NSLocale currentLocale];

// Set the date components you want
NSString *dateComponents = @"EEEEMMMMd";

// The components will be reordered according to the locale
NSString *dateFormat = [NSDateFormatter dateFormatFromTemplate:dateComponents options:0 locale:currentLocale];
NSLog(@"Date format for %@: %@", [currentLocale displayNameForKey:NSLocaleIdentifier value:[currentLocale localeIdentifier]], dateFormat);

NSDateFormatter *dateFormatter =[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:dateFormat];

NSString *formattedDateString = [dateFormatter stringFromDate:testdate];
NSLog(formattedDateString);
Run Code Online (Sandbox Code Playgroud)

特别感谢:http://oleb.net/blog/2011/11/working-with-date-and-time-in-cocoa-part-2/


fro*_*ouo 14

斯威夫特3

    let template = "EEEEdMMM"

    let format = DateFormatter.dateFormat(fromTemplate: template, options: 0, locale: NSLocale.current)
    let formatter = DateFormatter()
    formatter.dateFormat = format

    let now = Date()
    let whatYouWant = formatter.string(from: now) // ex: Sunday, Mar 5
Run Code Online (Sandbox Code Playgroud)

template符合您的需求.

这里的文档和示例可帮助您确定所需的模式.


Avi*_*oss 8

给@Joss一个很好的Swift扩展答案:

extension NSDate {
    func formattedFromCompenents(styleAttitude: NSDateFormatterStyle, year: Bool = true, month: Bool = true, day: Bool = true, hour: Bool = true, minute: Bool = true, second: Bool = true) -> String {
        let long = styleAttitude == .LongStyle || styleAttitude == .FullStyle
        var comps = ""

        if year { comps += long ? "yyyy" : "yy" }
        if month { comps += long ? "MMMM" : "MMM" }
        if day { comps += long ? "dd" : "d" }

        if hour { comps += long ? "HH" : "H" }
        if minute { comps += long ? "mm" : "m" }
        if second { comps += long ? "ss" : "s" }

        let format = NSDateFormatter.dateFormatFromTemplate(comps, options: 0, locale: NSLocale.currentLocale())
        let formatter = NSDateFormatter()
        formatter.dateFormat = format
        return formatter.string(from: self)
    }
}
Run Code Online (Sandbox Code Playgroud)