NSDate in full style but without year
Asked Answered
K

6

36

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.

Kudva answered 14/1, 2010 at 12:57 Comment(0)
A
39

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/

Abort answered 26/11, 2013 at 14:20 Comment(0)
A
20

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.

Auklet answered 2/3, 2017 at 17:0 Comment(1)
This is perfect. Thanks.Sashasashay
S
9

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)
    }
}
Sathrum answered 25/6, 2015 at 12:39 Comment(0)
S
4

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)
    }
}
Skysail answered 2/10, 2017 at 19:5 Comment(0)
W
0

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)
    }
}
Webby answered 29/10, 2021 at 23:5 Comment(0)
W
-6

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)
Womanhater answered 12/7, 2016 at 7:44 Comment(1)
The question specifically states "I want to show the date in the right region format".Breland

© 2022 - 2024 — McMap. All rights reserved.