Reputation: 47
How do i get first and last year of this decade ? Can someone pls help me regarding this?
I can get 1st last date of this year, but what about this decade? here is my code
[dateFormat setDateFormat:@"yyyy"];
NSString *theDateFormatted = [dateFormat stringFromDate: Sdate];
theDateFormatted = [NSString stringWithFormat:@"%@%@", theDateFormatted, @"-01-01 12:00:00 AM"];
// set last of month
NSString *theDateFormattedE = [dateFormat stringFromDate: Sdate];
theDateFormattedE = [NSString stringWithFormat:@"%@%@", theDateFormattedE, @"-12-31 11:59:59 PM"];
[dateFormat setDateFormat:@"yyyy-MM-dd hh:mm:ss a"];
Edate = [dateFormat dateFromString:theDateFormattedE];
Sdate = [dateFormat dateFromString:theDateFormatted];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"DB Status" message: theDateFormattedE delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
Upvotes: 0
Views: 142
Reputation: 5448
First get the current year from the date, like so :
NSDate *currentDate = [NSDate date];
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents* components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:currentDate]; // Get necessary date components
NSInteger year = [components year];
Then, to get the start year of the decade,
NSInteger firstYearOfTheDecade = year - (year % 10); //Add 1 to this if you want to start from x1
and for the last year
NSInteger lastYearOfTheDecade = firstYearOfTheDecade + 9;
Upvotes: 1