0

I stuck in problem. When i put break point and show value of nsdate object, it shows correct but when i print it in NSLog() it display other time.

code:

    NSDateFormatter *f = [[NSDateFormatter alloc] init];
    [f setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    [f setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
    NSDate *startDate = [f dateFromString:@"2014-01-17 15:30:00"];
    NSLog(@"tim %@",startDate);

Image of breakpoint in xcode

enter image description here

nslog o/p:

2014-01-17 11:27:08.348 APP[596:a0b] tim 2014-01-17 10:00:00 +0000

Is it normal behaviour or bug of xcode?

Edit:

I am creating LocalNotification based app so i want to use this as global time, so if my local is different it should be fire at i.e. 2014-01-17 15:30:00 , thats all.

Edit 2

As per answers and comment so use timezone so what is difference of NSLocale and NSTimeZone , because i have set nslocal to en_US.?

if i use defaults it will not let me problem?? i have fire localnotification at some perticuler time in all contry , so may it affect of time of call local notification?

  • 2
    Time zone difference. IST is India Standard Time which is UTC + 5:30. – Trausti Kristjansson Jan 17 '14 at 06:04
  • What is displayed is just ok. As you have displayed the date without mentioning the local thus it took the UTC. You can see it in your output as: **+0000** – Deepak Bharati Jan 17 '14 at 06:06
  • if i use UTC it will called in all contry at same time?? –  Jan 17 '14 at 06:12
  • Check my answers in these links http://stackoverflow.com/questions/15240170/nsdate-object-reciving-fron-nsstring-with-different-values/15240375#15240375 , http://stackoverflow.com/questions/15086781/nsdataformatter-datefromstring-results-in-incorrect-output/15089025#15089025 Hope this helps. – Meera Jan 17 '14 at 06:16

3 Answers3

0

You have to set time zone to UTC. Like this

    NSDateFormatter *f = [[NSDateFormatter alloc] init];
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
    [f setTimeZone:timeZone];
    [f setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *startDate = [f dateFromString:@"2014-01-17 15:30:00"];
    NSLog(@"tim %@",startDate);

I think this code solve your local notification fire time issue too.

if you want get date and time of your current location then you can use bellow code. This may help you.

    NSDate* sourceDate = [NSDate date];

    NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
    NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];

    NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
    NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
    NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;

    NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate] autorelease];

    NSTimeZone *timeZone2 = [NSTimeZone systemTimeZone];
    [f setTimeZone:timeZone2];

    NSLog(@"%@", destinationDate);
Bhavin_m
  • 2,746
  • 3
  • 30
  • 49
0

Just add this before logging the startDate:

NSLocale* local = [NSLocale currentLocale];

And the use the following log:

NSLog(@"time: %@", [startDate descriptionWithLocale: local]);

Edit:

-(NSDate *) getLocalTimeFrom:(NSDate *)UTCTime
{
   NSTimeZone *tz = [NSTimeZone defaultTimeZone];
   NSInteger seconds = [tz secondsFromGMTForDate: UTCTime];
   return [NSDate dateWithTimeInterval: seconds sinceDate: UTCTime];
} 

-(NSDate *) toGlobalTimeFrom:(NSDate *)localTime
{
   NSTimeZone *tz = [NSTimeZone defaultTimeZone];
   NSInteger seconds = -[tz secondsFromGMTForDate: localTime];
   return [NSDate dateWithTimeInterval: seconds sinceDate: localTime];
}
Deepak Bharati
  • 280
  • 2
  • 13
  • if i use UTC it will called in all contry at same time?? –  Jan 17 '14 at 06:13
  • I would like to recommend you not to hard code the local with some identifier. Let it use the default local and timezone. Everything will be taken care by its own. – Deepak Bharati Jan 17 '14 at 06:21
  • if i use defaults it will not let me problem?? i have fire localnotification at some perticuler time in all contry , so may it affect of time of call local notification? –  Jan 17 '14 at 06:24
  • In case if you need some conversion of date based on timezone you can use the stuff I added in the latest answer. – Deepak Bharati Jan 17 '14 at 06:25
  • you are wrong, if i'll set defalt and convert to date using formatter, it will give in correct date if user set 24 hour date. –  Jan 17 '14 at 06:27
  • did you see the latest added code to answer...? – Deepak Bharati Jan 17 '14 at 06:28
  • yes, but actually i didnt undrstd your edited answer.plz can you explain me it. –  Jan 17 '14 at 06:30
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/45433/discussion-between-deepak-bharati-and-user3057005) – Deepak Bharati Jan 17 '14 at 06:32
-1

Try this

NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
[f setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
NSDate *startDate = [f dateFromString:@"2014-01-17 15:30:00"];
NSLog(@"tim %@", [f stringFromDate:startDate]);
Akhilrajtr
  • 5,170
  • 3
  • 19
  • 30