Great help for NSCalendar..

NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:[[NSDate alloc] init]];

[components setHour:-[components hour]];
[components setMinute:-[components minute]];
[components setSecond:-[components second]];
NSDate *today = [cal dateByAddingComponents:components toDate:[[NSDate alloc] init] options:0]; //This variable should now be pointing at a date object that is the start of today (midnight);

[components setHour:-24];
[components setMinute:0];
[components setSecond:0];
NSDate *yesterday = [cal dateByAddingComponents:components toDate: today options:0];

components = [cal components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:[[NSDate alloc] init]];

[components setDay:([components day] – ([components weekday] – 1))];
NSDate *thisWeek = [cal dateFromComponents:components];

[components setDay:([components day] – 7)];
NSDate *lastWeek = [cal dateFromComponents:components];

[components setDay:([components day] – ([components day] -1))];
NSDate *thisMonth = [cal dateFromComponents:components];

[components setMonth:([components month] – 1)];
NSDate *lastMonth = [cal dateFromComponents:components];


This entry was posted in iOS by Muzammil. Bookmark the permalink.

About Muzammil

Hey Guys, I am Muzammil Mohammad. Having 7 years of experience in iOS development. I am here to share my knowledge with all mobile application developers and those companies who are stuck on development (due to bad quality of code or don't know what to do) or want to know how to build a good mobile app. I believe I am more expert in UI and Architecture design of mobile app. So if you need my helps or assistant for your mobile app projects, Feel free to contact me. I will try my best to improve the quality of your app. Or you can hire me to develop your apps. I’d love to talk with you. Best!

Leave a Reply

Your email address will not be published. Required fields are marked *