when calling method for simulator version 6.1 it shows:
year---2012
week no---52
weekday---2
resultdate---2012-12-16 18:30:00 +0000
date time is 17 Dec 2012 To 23 Dec 2012
version 5.0 it shows:
year---2012
week no---52
weekday---2
resultdate---2012-12-23 18:30:00 +0000 date time is 24 dec. 2012 To 30 dec. 2012
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
gregorian.Locale = [NSLocale currentLocale];
//[gregorian setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
gregorian.firstWeekday = 2; // Sunday = 1, Saturday = 7
NSDateComponents *components = [gregorian components:NSYearForWeekOfYearCalendarUnit |NSWeekOfYearCalendarUnit |NSWeekdayOrdinalCalendarUnit fromDate:[NSDate date]];
[components setYearForWeekOfYear:2012];
[components setWeekOfYear:52];
[components setWeekdayOrdinal:2];
NSLog(@"year---%ld",(long)[components yearForWeekOfYear]);
NSLog(@"week no---%ld",(long)[components weekOfYear]);
NSLog(@"weekday---%ld",(long)[components weekdayOrdinal]);
NSDate *resultDate = [gregorian dateFromComponents:components];
NSLog(@"resultdate---%@",resultDate);
NSDate *startOfTheWeek;
NSDate *endOfWeek;
NSTimeInterval interval;
[gregorian rangeOfUnit:NSWeekOfYearCalendarUnit startDate:&startOfTheWeek interval:&interval forDate:resultDate];
endOfWeek = [startOfTheWeek dateByAddingTimeInterval:interval-1];
and to get the out put dateformatter used is
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd MMM yyyy"];
NSString *str_StartOfTheWeek = [dateFormatter stringFromDate:startOfTheWeek];
NSString *str_EndOfWeek = [dateFormatter stringFromDate:endOfWeek];
NSLog(@"date time is %@",[NSString stringWithFormat:@"%@ To %@",str_StartOfTheWeek,str_EndOfWeek]);
return [NSString stringWithFormat:@"%@ To %@",str_StartOfTheWeek,str_EndOfWeek];
how to solve this issue pls help.
To set a weekday (e.g. 2 = Monday) you have to use the NSWeekdayCalendarUnit
and setWeekday:
, not NSWeekdayOrdinalCalendarUnit
and setWeekdayOrdinal:
:
NSDateComponents *components = [gregorian components:NSYearForWeekOfYearCalendarUnit|NSWeekOfYearCalendarUnit|NSWeekdayCalendarUnit
fromDate:[NSDate date]];
[components setYearForWeekOfYear:2012];
[components setWeekOfYear:52];
[components setWeekday:2];
With this modification, the output is
date time is 17 Dec 2012 To 23 Dec 2012
on both iOS 5 and iOS 6.
when calling method for simulator version 6.1 it shows:
year---2012
week no---52
weekday---2
resultdate---2012-12-16 18:30:00 +0000
date time is 17 Dec 2012 To 23 Dec 2012
version 5.0 it shows:
year---2012
week no---52
weekday---2
resultdate---2012-12-23 18:30:00 +0000 date time is 24 dec. 2012 To 30 dec. 2012
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
gregorian.Locale = [NSLocale currentLocale];
//[gregorian setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
gregorian.firstWeekday = 2; // Sunday = 1, Saturday = 7
NSDateComponents *components = [gregorian components:NSYearForWeekOfYearCalendarUnit |NSWeekOfYearCalendarUnit |NSWeekdayOrdinalCalendarUnit fromDate:[NSDate date]];
[components setYearForWeekOfYear:2012];
[components setWeekOfYear:52];
[components setWeekdayOrdinal:2];
NSLog(@"year---%ld",(long)[components yearForWeekOfYear]);
NSLog(@"week no---%ld",(long)[components weekOfYear]);
NSLog(@"weekday---%ld",(long)[components weekdayOrdinal]);
NSDate *resultDate = [gregorian dateFromComponents:components];
NSLog(@"resultdate---%@",resultDate);
NSDate *startOfTheWeek;
NSDate *endOfWeek;
NSTimeInterval interval;
[gregorian rangeOfUnit:NSWeekOfYearCalendarUnit startDate:&startOfTheWeek interval:&interval forDate:resultDate];
endOfWeek = [startOfTheWeek dateByAddingTimeInterval:interval-1];
and to get the out put dateformatter used is
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd MMM yyyy"];
NSString *str_StartOfTheWeek = [dateFormatter stringFromDate:startOfTheWeek];
NSString *str_EndOfWeek = [dateFormatter stringFromDate:endOfWeek];
NSLog(@"date time is %@",[NSString stringWithFormat:@"%@ To %@",str_StartOfTheWeek,str_EndOfWeek]);
return [NSString stringWithFormat:@"%@ To %@",str_StartOfTheWeek,str_EndOfWeek];
how to solve this issue pls help.
To set a weekday (e.g. 2 = Monday) you have to use the NSWeekdayCalendarUnit
and setWeekday:
, not NSWeekdayOrdinalCalendarUnit
and setWeekdayOrdinal:
:
NSDateComponents *components = [gregorian components:NSYearForWeekOfYearCalendarUnit|NSWeekOfYearCalendarUnit|NSWeekdayCalendarUnit
fromDate:[NSDate date]];
[components setYearForWeekOfYear:2012];
[components setWeekOfYear:52];
[components setWeekday:2];
With this modification, the output is
date time is 17 Dec 2012 To 23 Dec 2012
on both iOS 5 and iOS 6.
0 commentaires:
Enregistrer un commentaire