33

我正在使用适用于 iPhone 的 Objective C 并且有一个我想以完整样式显示但没有年份的 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

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

4

6 回答 6

38

我知道这是一个很老的问题,但这是我在 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);

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

于 2013-11-26T14:20:46.983 回答
20

斯威夫特 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

玩以template满足您的需求。

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

于 2017-03-02T17:00:48.040 回答
9

@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)
    }
}
于 2015-06-25T12:39:52.847 回答
4

斯威夫特 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)
    }
}
于 2017-10-02T19:05:03.927 回答
0

斯威夫特 5

来自@Joss 回复:

extension Date {
    
    func output(_ components: String = "EEEEMMMMd") -> String{
        
        let currentLocale: Locale = Locale.current

        // Set the date components you want
        let dateComponents: String = components

        // The components will be reordered according to the locale
        let dateFormat = DateFormatter.dateFormat(fromTemplate: dateComponents, options: 0, locale: currentLocale)

        let formatter =  DateFormatter()
        
        formatter.dateFormat = dateFormat

        return formatter.string(from: self)
    }
}
于 2021-10-29T23:05:09.897 回答
-6

这是一个在标签上仅显示月份日期的快速选项:

let todaysDate: NSDate = NSDate()
let formatter = NSDateFormatter()
formatter.dateFormat = "MMMM dd"
self.todayLabel.text = formatter.stringFromDate(todaysDate)
于 2016-07-12T07:44:37.513 回答