Nextrp [CPP] RU + Many GEOs Игра на карте России | NEXTRP

How to find Next Sunday in different Timezone (EST & IST)

ios objective-c timezone nsdate

88 просмотра

4 ответа

179 Репутация автора

I have to find next Sunday date (NSDate) from device's current date.

I have used below logic:

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [calendar components:NSCalendarUnitWeekday | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:self];
NSUInteger weekdayToday = [components weekday];

NSInteger daysToMonday = (8 - weekdayToday) % 7;

NSDate *weekEndPrev = [self dateByAddingTimeInterval:60*60*24*daysToMonday];

Here, in EST if time is near to 11 PM, I'm getting Monday as Weekend.

Let me know feasible solution. I have tried many options. Nothings works with 2 different timezones.

Thanks in Advance.

Автор: Dimple Shah Источник Размещён: 07.03.2017 08:13

Ответы (4)

0 плюса

1765 Репутация автора

You need just to get the number of days you need and then just add them to your current date:

    NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDate * newDate = [calendar dateByAddingUnit:NSCalendarUnitDay value:value toDate:date options:0];

where value - is the number of days to add

Автор: Nazir Размещён: 07.03.2017 08:35

0 плюса

2495 Репутация автора

Find current week Sunday Date first then add 7 days using NSDateComponents.

NSDate *today = [[NSDate alloc] init];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *weekdayComponents = [gregorian components:NSCalendarUnitWeekday fromDate:today];
NSDateComponents *componentsToSubtract = [[NSDateComponents alloc] init];
[componentsToSubtract setDay:(0 - ([weekdayComponents weekday] - 1))];
NSDate *sudayCurWeek = [gregorian dateByAddingComponents:componentsToSubtract toDate:today options:0];
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setDay:7];
NSDate *nextSunday = [gregorian dateByAddingComponents:offsetComponents toDate:sudayCurWeek options:0];
Автор: kb920 Размещён: 07.03.2017 11:04

0 плюса

46526 Репутация автора

You can do this without any math, which is best left to NSCalendar as it handles daylight savings etc. First you need a calendar with the correct time zone. For example for IST:

NSCalendar *gc = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
gc.timeZone = [NSTimeZone timeZoneWithName:@"IST"];

Then you can use nextDateAfterDate:matchingUnit:value:options: to find the start of the next Sunday after a given time. For example:

NSDate *start = ... // the time you wish to start from
NSDate *nextSunday =
    [gc nextDateAfterDate:start
                 matching:UnitNSCalendarUnitWeekday // match based on weekday number
                    value:1                         // Sunday = weekday 1
                  options:NSCalendarMatchNextTime   // read the docs!

The returned date, nextSunday, will be the next Sunday at 00:00 in the timezone of the calendar, gc.


Автор: CRD Размещён: 07.03.2017 09:03

0 плюса

179 Репутация автора

Finally, I got solution..

NSDateComponents *components = [[NSCalendar currentCalendar] componentsInTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"] fromDate:[NSDate date]]; // Pass date for which you want to find next Sunday
NSUInteger weekdayToday = [components weekday];
NSInteger daysToMonday = (8 - weekdayToday) % 7;
NSDate *weekEndPrev = [[NSDate date] dateByAddingTimeInterval:60*60*24*daysToMonday];
Автор: Dimple Shah Размещён: 09.03.2017 07:20
Вопросы из категории :