NSDate 完整样式但没有年份

发布于 2024-08-17 15:54:02 字数 700 浏览 5 评论 0原文

我正在使用 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 中不同字母代表什么的信息。

I'm working with Objective C for iPhone and have a NSDate that I want to display in full style but without the year. Right now I'm using the code below but it also show the year, and I don't want that. As I want to show the date in the right region format I cannot just delete the year in the end as in some countries the year will not be in the end but in the middle or beginning.

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

What is the solution for this? And another question, where can I find information on how to use the setDateFormat of NSDateFormatter? I can't find information about what different letters stand for in the API.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

ヤ经典坏疍 2024-08-24 15:54:02

我知道这是一个很老的问题,但这是我在 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 -可可时间-第2部分/

I know this is a pretty old question but this is the only one I found on SO that talked about the same issue I was having.

The key method to use here is: dateFormatFromTemplate

Instead of setting the style, you want to set the format as such:

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

Special thanks to: http://oleb.net/blog/2011/11/working-with-date-and-time-in-cocoa-part-2/

白馒头 2024-08-24 15:54:02

Swift 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 来满足您的需求。

此处的文档和示例可帮助您确定你想要的图案。

Swift 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

Play with template to match your needs.

Doc and examples here to help you determine the pattern you want.

池木 2024-08-24 15:54:02

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

Took @Joss great answer to a Swift extension:

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)
    }
}
深陷 2024-08-24 15:54:02

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

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)
    }
}
不羁少年 2024-08-24 15:54:02

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

Swift 5

From @Joss reply:

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)
    }
}
述情 2024-08-24 15:54:02

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

let todaysDate: NSDate = NSDate()
let formatter = NSDateFormatter()
formatter.dateFormat = "MMMM dd"
self.todayLabel.text = formatter.stringFromDate(todaysDate)

Here's a swift option to show just Month Date on a Label:

let todaysDate: NSDate = NSDate()
let formatter = NSDateFormatter()
formatter.dateFormat = "MMMM dd"
self.todayLabel.text = formatter.stringFromDate(todaysDate)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文