Reputation: 1573
I have created a weekly view(similar to calendar) with header day of week. I am creating the current time using below code :
+(NSDate *) getCurrentDate {
NSDate* datetime = [NSDate date];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"America/Juneau"];// this is dynamic and coming from server.
[dateFormatter setTimeZone:timeZone]; // Prevent adjustment to user's local time zone.
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSString* dateTimeInIsoFormatForUserTimeZone = [dateFormatter stringFromDate:datetime];
return [dateFormatter dateFromString:dateTimeInIsoFormatForUserTimeZone];
}
I am using this date as reference to create week date (from monday to sunday). My code work fine for all case but I was failing at DST time zone.
I have seen similar question on stack-overflow but they are more subjective and not able to solve my problem. Answer with code and best practise is highly appreciated. Thanks In advance!.
Upvotes: 0
Views: 1175
Reputation: 2824
[[NSTimeZone *yourTimeZone*] secondsFromGMTForDate:*yourDate*]
With this method, you can get secondsFromGMTForDate
for a particular timezone
which is also adjusted with daylight saving.
You can use this offset to create a date object in your timezone and show the weekview accordingly.
Upvotes: 2