-1

I was trying to get the date alone (without time) from today's date using this code:

NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *date1Components = [cal components: NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:[NSDate date]];

NSLog(@"Today without time :%@",[cal dateFromComponents:date1Components]);

The result is:

2014-10-14 23:01:13.398 TestViewHier[5349:60b] Today without time :2014-10-13 18:30:00 +0000

I am from India, now the time is: 2014-10-14 11:16 pm, but the result shows one day off and some random hour also.

My expected result is 2014-10-14 00:00:00, because I need to compare this date with another date. I will get a product's expiration date from a service, and if the date is less than or equal to the current date, I need to display the products in an "Expired Products" section. I can compare the two dates by using NSDate's comparison method.

jscs
  • 63,694
  • 13
  • 151
  • 195
Prasad
  • 1,904
  • 4
  • 19
  • 24
  • We can tell you're from India because it's the only major timezone which sits on a half-hour boundary relative to UTC. – Hot Licks Oct 14 '14 at 19:13
  • [Please don't bother adding "Thanks" and the like to your posts](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts). You can thank answerers by upvoting if their answers are helpful. – jscs Oct 14 '14 at 19:46

2 Answers2

1

NSDate is always a point in time in UTC (universal time).

You took your current time.

You extract year, month and day using your Indian calendar, so you get the values that you expect.

You convert these components to an NSDate. This gives a point in time at the beginning of your day in India. Because you are 5 1/2 hours away from UTC, that point in time in UTC would be the previous day at 18:30. In other words, if you had called me on the phone at the start of the day (midnight), and I looked at my calendar and my watch, my calendar would show the previous day, and my watch would show 18:30.

And that is what NSLog displays. The NSDate is absolutely correct; it is the start of the day according to your calendar. Your day started at 18:30 UTC.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
  • OK, but the question is asking how to get his current date (for his time zone), without the time – JoelFan Oct 14 '14 at 18:04
  • @JoelFan: As gnasher has said, OP has what he wants already; a date does not include time zone information. It's _displayed_ in GMT when it's printed out, but the moment in time that it represents _is_ the start of the day 2014-10-14 in OP's time zone. – jscs Oct 14 '14 at 19:07
  • @gnasher729: I have executed the above code at 11:16 pm(not the start of the day, its almost end of the day), which means 5:30 pm UTC on the same day. How come it is showing one day off – Prasad Oct 14 '14 at 19:25
  • @JoelFan: What does it mean by OP time zone? – Prasad Oct 14 '14 at 19:31
  • @JoshCaswell, a date (even without the time) certainly is affected by the time zone. OP wants the date to be correct for his time zone, just not include the time – JoelFan Oct 14 '14 at 19:57
  • No, the _display_ of the date is affected by the time zone, @JoelFan, but the date 2014-10-14T00:00:00-5:30 is the **same date** as 2014-10-13T18:30:00+0:00. – jscs Oct 14 '14 at 20:03
  • @JoelFan, observe: https://gist.github.com/woolsweater/acfdac54b7b1f23856bb – jscs Oct 14 '14 at 20:39
0

To get a string with the date without the time use NSDateFormatter method: startOfDayForDate available in OSX 10.9 and iOS 8.0.

Example:

NSDate *date = [NSDate date];
NSLog(@"date:           %@", date);

NSDate *dateOnlyString = [[NSCalendar currentCalendar] startOfDayForDate:date];
NSLog(@"dateOnlyString: %@", dateOnlyString);

Output:

date:           2014-10-14 18:11:43 +0000  
dateOnlyString: 2014-10-14 04:00:00 +0000

But the real problem may be that NSLog uses the NSDate description method. That displays the date/time based on several criteria you may not want. If you want to see the date in a specific format use a NSDateFormatter to create the display string.

zaph
  • 111,848
  • 21
  • 189
  • 228