-2

I am using NSDateFormatter to convert NSString into NSDate according to local timezone. but it always returns in UTC. I am using this code:

NSString *dateString = @"28-12-2015 04:58:10 PM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[dateFormatter setDateFormat:@"dd-MM-yyyy hh:mm:ss a"];
dateFormatter.timeZone = [NSTimeZone localTimeZone];
NSDate *dateFromString = [[NSDate alloc] init];
// voila!
dateFromString = [dateFormatter dateFromString:dateString];

In console I am getting this:

Printing description of dateFromString:
2015-12-28 11:28:10 +0000

Please help me to resolve this issue.

Sohil R. Memon
  • 9,404
  • 1
  • 31
  • 57
Sudha Tiwari
  • 2,499
  • 26
  • 50

3 Answers3

1

Try changing the time zone to GMT

NSString *dateString = @"28-12-2015 04:58:10 PM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy hh:mm:ss a"];    
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
dateFromString = [dateFormatter dateFromString:dateString];

In console I got

2015-12-28 16:58:10 +0000
Arun
  • 1,391
  • 1
  • 10
  • 29
1

Every time you print NSDate either through NSLog or print or PO or anyother way it will print in GMT, there is no way to alter NSDate to save the date in your desired way.

What you can do it change the format using NSDateFormatter and store the date as string and use it.

Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
-1
NSDate* date = nil;
NSDateFormatter*dateformatter = [[NSDateFormatter alloc] init];
[dateformatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
date = [dateformatter dateFromString:time];
NSInteger interval = [[NSTimeZone defaultTimeZone] secondsFromGMT];
date = [date dateByAddingTimeInterval:interval];
Bhadresh Mulsaniya
  • 2,610
  • 1
  • 12
  • 25