Iphone NSDate完整样式,但无年份

Iphone NSDate完整样式,但无年份,iphone,nsdate,nsdateformatter,Iphone,Nsdate,Nsdateformatter,我正在使用Objective C for iPhone,并且有一个NSDate,我希望以完整的样式显示,但没有年份。现在我正在使用下面的代码,但它也显示了年份,我不希望这样。由于我想以正确的区域格式显示日期,所以我不能只删除最后一年,因为在一些国家,一年不会结束,而是在中间或开始。< /P> NSDate *testdate = [NSDate date]; NSDateFormatter *dateFormattertest = [[NSDateFormatter alloc] init];

我正在使用Objective C for iPhone,并且有一个NSDate,我希望以完整的样式显示,但没有年份。现在我正在使用下面的代码,但它也显示了年份,我不希望这样。由于我想以正确的区域格式显示日期,所以我不能只删除最后一年,因为在一些国家,一年不会结束,而是在中间或开始。< /P>
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

解决这个问题的办法是什么?还有一个问题,我在哪里可以找到关于如何使用NSDateFormatter的setDateFormat的信息?我找不到关于API中不同字母代表什么的信息。

我知道这是一个非常老的问题,但这是我在上面找到的唯一一个关于我所遇到的相同问题的问题

这里要使用的关键方法是: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);

特别感谢:

给@Joss一个快速扩展的好答案:

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)
    }
}

这里有一个swift选项,可在标签上仅显示月份日期:

let todaysDate: NSDate = NSDate()
let formatter = NSDateFormatter()
formatter.dateFormat = "MMMM dd"
self.todayLabel.text = formatter.stringFromDate(todaysDate)
斯威夫特3 使用
模板
来满足您的需要


文档和示例帮助您确定所需的模式。

Swift 4

extension Date {
    public func formattedFromComponents(styleAttitude: DateFormatter.Style, year: Bool = false, month: Bool = false, day: Bool = false, hour: Bool = false, minute: Bool = false, second: Bool = false, locale: Locale = Locale.current) -> String {
        let long = styleAttitude == .long || styleAttitude == .full
        let short = styleAttitude == .short
        var comps = ""

        if year { comps += long ? "yyyy" : "yy" }
        if month { comps += long ? "MMMM" : (short ? "MM" : "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 = DateFormatter.dateFormat(fromTemplate: comps, options: 0, locale: locale)
        let formatter = DateFormatter()
        formatter.dateFormat = format
        return formatter.string(from: self)
    }
}

这个问题特别指出“我想以正确的区域格式显示日期”。这很完美。谢谢
extension Date {
    public func formattedFromComponents(styleAttitude: DateFormatter.Style, year: Bool = false, month: Bool = false, day: Bool = false, hour: Bool = false, minute: Bool = false, second: Bool = false, locale: Locale = Locale.current) -> String {
        let long = styleAttitude == .long || styleAttitude == .full
        let short = styleAttitude == .short
        var comps = ""

        if year { comps += long ? "yyyy" : "yy" }
        if month { comps += long ? "MMMM" : (short ? "MM" : "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 = DateFormatter.dateFormat(fromTemplate: comps, options: 0, locale: locale)
        let formatter = DateFormatter()
        formatter.dateFormat = format
        return formatter.string(from: self)
    }
}