0

Actually in my simulater TIME displays past 5 hrs 30 mins

My current time in india is 2013-04-02 09:10:__ AM But the output is 2013-04-02 03:54:51 +0000

2013- April - 2nd : 9:10 AM

I need to get some results between two dates 1.First Date of month to 2.Current Date of any day

ie from April 1st to April 2nd 9.10 AM t0 current time 2013-04-02 09:10:__ AM

but it dispalys

Date of first day of month as 2013-03-31 18:30:00 +0000

Current Time as 2013-04-02 03:54 :51 +0000 but the actual time is 09:10:__ AM

I compute first date of the month as follows

// TO IMPLEMENT CODE for FIRST DAY OF the Month
      NSCalendar* calendar = [NSCalendar currentCalendar];
      NSDateComponents* comps = [calendar components:NSYearForWeekOfYearCalendarUnit |NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:[NSDate date]];

 calendar.timeZone = [NSTimeZone systemTimeZone];

      [comps setWeekday:1]; // 1: sunday
      monthComponent = comps.month;
      yearComponent = comps.year;
      dateFormatter = [[NSDateFormatter alloc] init];
      [dateFormatter setDateFormat:@"yyyy MM dd"];

 dateFormatter.timeZone = [NSTimeZone systemTimeZone];

      // To get the firstDateOfMonth for This MONTH.
      NSDate *firstDateOfMonth = [dateFormatter dateFromString:[NSString stringWithFormat:@"%d %d 01",yearComponent,monthComponent]];

      NSLog(@"firstDateOfMonth-- %@ \r\n",firstDateOfMonth);
       NSLog(@"[NSDate date]-- %@",[NSDate date]);

I set timezone SystemTimeZone even thought i got wrong results for Current date and firstDateOfMonth

Thanks in advance

iOS dev
  • 2,254
  • 6
  • 33
  • 56

4 Answers4

0

The problem time zones. By default, a NSDateFormatter is set to your local timezone. That means, if your time zone is +5:30 giving it a date "1970/18/3" results in 1970-03-18 00:00:00 +0530. However, NSLog always prints dates in GMT (zero) time zone, adding/substracting the time zone difference (5 hours and 30 minutes).

 NSCalendar* calendar = [NSCalendar currentCalendar];
    NSDateComponents* comps = [calendar components:NSYearForWeekOfYearCalendarUnit |NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:[NSDate date]];


    [comps setWeekday:1]; 
    int monthComponent = comps.month;
   int yearComponent = comps.year;
   NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy MM dd"];

    NSDate *firstDateOfMonth = [dateFormatter dateFromString:[NSString stringWithFormat:@"%d %d 01",yearComponent,monthComponent]];
    firstDateOfMonth = [firstDateOfMonth dateByAddingTimeInterval:19800];
    NSLog(@"firstDateOfMonth-- %@ \r\n",firstDateOfMonth);
    NSLog(@"[NSDate date]-- %@",[[NSDate date] dateByAddingTimeInterval:19800]);
Balu
  • 8,470
  • 2
  • 24
  • 41
0

Try it....

NSDate* date = [NSDate date];
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
NSTimeZone *destinationTimeZone = [NSTimeZone systemTimeZone];
formatter.timeZone = destinationTimeZone;
[formatter setDateStyle:NSDateFormatterLongStyle];
[formatter setDateFormat:@"MM/dd/yyyy hh:mma"];
NSString* dateString = [formatter stringFromDate:date];
Chirag Pipaliya
  • 1,281
  • 12
  • 20
0

The problem is time zones, as Sunny said, plus a misunderstanding of what NSDate actually means.

NSDate represents points in time which are the same everywhere in the world. If we call [NSDate date] at the same moment, we get the same result, and NSLog prints it in the same way. However, if we both look at our watches, we see different times displayed.

You are in India. If you look at your watch at exactly midnight, it shows 12pm. If a person in London looks at their watch at the exact same moment in time, that person will see 6:30pm. Their watch is always 5 1/2 hours behind yours. And that's what NSLog displays.

For example, you calculated correctly the NSDate for the beginning of the month in India. The beginning of the month was April 1st, at midnight at the beginning of the day - in Indian time. But at that moment in time, it is still the 31st of March in London, at 18:30. And that's what NSDate displays. You got the correct result all the time.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
-1

@IOS dev,

May be it is a timezone issue. Check your simulator settings. Use below link:

xcode 4.2 location simulator and Time Zone Also check with [NSTimeZone localTimeZone].

See below link:

NSTimeZone: what is the difference between localTimeZone and systemTimeZone?

Community
  • 1
  • 1
Ramu Pasupuleti
  • 888
  • 2
  • 15
  • 35