1

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?

Nazik
  • 8,696
  • 27
  • 77
  • 123
  • 1
    see this link http://stackoverflow.com/questions/23845647/how-to-manage-nsdate-for-different-timezones – Anbu.Karthik Nov 07 '15 at 08:46
  • If the initial string has the date from a specific time zone, always set your date formatter to use that time zone. If the date string is supposed to reflect local time, you need to rethink what you're trying to accomplish. – Avi Nov 08 '15 at 08:39

0 Answers0