IOS Get Current Weekday with NSDate


/ Published in: iPhone



Copy this code and paste it in your HTML
  1. // get the current date
  2. NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
  3. [dateFormat setDateFormat:@"yyyy-MM-dd"];
  4. NSDate *now = [[NSDate alloc] init];
  5.  
  6. // get the weekday of the current date
  7. NSCalendar* cal = [NSCalendar currentCalendar];
  8. NSDateComponents* components = [cal components:NSWeekdayCalendarUnit fromDate:now];
  9. NSInteger weekday = [components weekday]; // 1 = Sunday, 2 = Monday, etc.

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.