0

i have used setDefaultTimezone to set the timezone and displayed the time and location in a UITableview. enter image description here

and i used below code to set default timezone

[cell.detailTextLabel setText:[NSString stringWithFormat:@"%@", evnt.location]];
[NSTimeZone setDefaultTimeZone:[NSTimeZone timeZoneWithName:evnt.location]];
[cell.textLabel setText:[NSString stringWithFormat:@"%@",[NSDate date]]];

what i have to do?

Muruganandham K
  • 5,271
  • 5
  • 34
  • 62

2 Answers2

1

Use NSCalendar, and the setTimeZone method.

  NSDate *newDate;
  NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:~              NSTimeZoneCalendarUnit fromDate:[NSDate date]];

  newDate = [[NSCalendar currentCalendar] dateFromComponents:dateComponents];
  NSLog(@"newDate: %@", newDate);
  NSLog(@"newDate: %.0f", [newDate timeIntervalSinceReferenceDate]);

newDate: 2011-09-09 15:02:09 +0000 newDate: 337273330

  [dateComponents setTimeZone:[NSTimeZone timeZoneWithName:@"Australia/Sydney"]];
  newDate = [[NSCalendar currentCalendar] dateFromComponents:dateComponents];
  NSLog(@"newDate: %@", newDate);
  NSLog(@"newDate: %.0f", [newDate timeIntervalSinceReferenceDate]);

newDate: 2011-09-09 00:52:03 +0000 newTimeInterval: 337222930

 [dateComponents setTimeZone:[NSTimeZone timeZoneWithName:@"Europe/Paris"]];
 newDate = [[NSCalendar currentCalendar] dateFromComponents:dateComponents];
 NSLog(@"newDate: %@", newDate);
 NSLog(@"newDate: %.0f", [newDate timeIntervalSinceReferenceDate]);

newDate: 2011-09-09 08:52:03 +0000 newTimeInterval: 337251730

Bhrigesh
  • 861
  • 1
  • 7
  • 14
0

-[NSDate description] always prints the date in UTC.

So try to do this:

NSDate *myDate = [[NSDate date] descriptionWithLocale:[NSLocale systemLocale]];

Plus check the below:

NSDate is not returning my local Time zone /default time zone of device

https://stackoverflow.com/questions/13260787/setdefaulttimezone-not-working

Convert UTC NSDate to local Timezone Objective-C

Community
  • 1
  • 1
Tarek Hallak
  • 18,422
  • 7
  • 59
  • 68