I need seven continuous dates' string from a single NSDate. This is what I did,
for (int i = 0; i < 7; i++) {
NSString *dayOfWeek = [self returnStringFromDate:currentDate withStringFOrmat:@"dd - EEEE"];
[daysOfWeekArray addObject:[dayOfWeek uppercaseString]];
currentDate = [currentDate dateByAddingTimeInterval:60 * 60 * 24];
}
- (NSString *)returnStringFromDate:(NSDate *)date
withStringFOrmat:(NSString *)stringFOrmat {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[dateFormatter setLocale:locale];
[dateFormatter setDateFormat:stringFOrmat];
NSString *dateString = [dateFormatter stringFromDate:date];
return dateString;
}
I'm from India, If I have my own time zone(Indian standard time) and passing the currentDate as 01/sun/nov/2015 - it's logging as 2015-10-31 18:30:00 +0000
, the above code works correctly. If I change my time zone to US time zone(CST), current date is logging as 2015-11-01 05:00:00 +0000
, it's returning one date before for the string. If I try
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:stringFOrmat];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
NSString *dateString = [dateFormatter stringFromDate:date];
it's working,
but how can I identify which time time zone am I now?
Is it possible to use some common code to find the correct date string from the current time zone of the device?