0

Considering the example from Convert string to date in my iPhone app

NSString *dateStr = @"Tue, 25 May 2010 12:53:58 +0000";

// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"EE, d LLLL yyyy HH:mm:ss Z"];
NSDate *date = [dateFormat dateFromString:dateStr]; 
[dateFormat release];

I checked what methods NSDate has and found
enter image description here

Question
How can I get Tue and 25 from *date?

Community
  • 1
  • 1
daydreamer
  • 87,243
  • 191
  • 450
  • 722
  • 5
    Look at `NSDateComponents`. – rmaddy Sep 11 '14 at 18:25
  • 1
    ... and to make some obvious-with-hindsight points to aid your learning: `NSDate`s don't have a day in them. They represent an instant in time (and, yes, perhaps don't have the best name: think of them as `NSTimestamp` if it's easier). So you need to get a calendar in there somewhere to impute the concept of numbered days relative to some start date. The output should differ depending on Gregorian, Julian, Chinese, Buddhist, etc, calendars and does. – Tommy Sep 11 '14 at 18:49
  • Off-topic: What is this documentation you're using, is it some sort of Xcode plugin? – chunkyguy Sep 11 '14 at 19:00
  • Its AppCode. http://www.jetbrains.com/objc/ – daydreamer Sep 11 '14 at 19:04

1 Answers1

3

You should use NSDateComponents for this:

NSCalendar * calendar = [NSCalendar currentCalendar];
NSDateComponents * dateComponents = [calendar components: NSDayCalendarUnit | NSWeekdayCalendarUnit fromDate: <yourDate>];

NSLog(@"Day in week: %ld", (long)dateComponents.weekday); // Day in week: 3
NSLog(@"Day in month: %ld", (long)dateComponents.day);    // Day in month: 25
NSLog(@"Day name: %@", calendar.shortWeekdaySymbols[dateComponents.weekday - 1]); // Day name: Tue (iOS 8.0 +)

Or in Swift

let calendar = NSCalendar.currentCalendar()
let dateComponents = calendar.components(.DayCalendarUnit | .WeekdayCalendarUnit, fromDate: <yourDate>)

println("Day in week: \(dateComponents.weekday)") // Day in week: 3
println("Day in month: \(dateComponents.day)")    // Day in month: 25
println("Day name: \(calendar.shortWeekdaySymbols[dateComponents.weekday - 1])") // Day name: Tue
Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
  • I get error `'shortWeekdaySymbols' is unavailable: not available on iOS 7.1` – daydreamer Sep 11 '14 at 18:53
  • 1
    `shortWeekdaySymbols` is a class method of `NSDateFormatter`, not `NSCalendar`. – rmaddy Sep 11 '14 at 20:00
  • It's an instance method on `NSCalendar` from iOS 8.0… see https://developer.apple.com/library/prerelease/ios/releasenotes/General/iOS80APIDiffs/frameworks/Foundation.html (and also an `NSDateFormatter` *instance* method prior to that) – Ashley Mills Sep 11 '14 at 20:45
  • Note that there's nothing wrong with just using NSDateFormatter. It's slightly less efficient, but most iOS programmers get to be familiar with it, so it's likely to code up faster. – Hot Licks Sep 11 '14 at 20:59
  • Maybe, but I'd suggest that any programmer taking more than a cursory glance at dates would familiar be with `NSDateComponents` (or should take this as a learning opportunity) – Ashley Mills Sep 11 '14 at 21:04
  • @AshleyMills Strange. The API diffs indicate the changes but the actual reference docs do not. – rmaddy Sep 11 '14 at 21:53