0

I have a current NSString in the format of 2010-04-23 00:00:00 and then I'm trying to get the number of days passed from the current day. However, I'm not sure how to handle when the user changes their locale to Thailand for example.

Here is some of the code.

NSString *start = @"2010-04-23 00:00:00";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSDate *date = [formatter dateFromString:start];

//Region Format Thailand
NSDate *today = [[NSDate alloc] init];
NSTimeInterval difference =  [today timeIntervalSinceDate:start];
int numberOfDays = difference / 86400;

What would be the correct way to handle this situation so the number of days difference is accurate?

aahrens
  • 5,522
  • 7
  • 39
  • 63
  • possible duplicate: http://stackoverflow.com/questions/1862905/nsdate-convert-date-to-gmt – Dima Sep 23 '13 at 17:49
  • The scenario he is describing is not at all like GMT "date conversion". He seems to be describing calculating days between dates, but potentially using different calendars. – quellish Sep 14 '14 at 22:01
  • @aahrens, are you talking about a "date" in thailand and the second date being in a different timezone/locale, or are both in thailand? Or are you talking about the device being in one locale, but you need the dates you are working with to use a different locale? Can you update your question with that information? – quellish Sep 14 '14 at 22:03
  • *You* have to decide whether the difference should be figured in absolute (UTC) terms or relative to local time. If absolute/UTC then you can simply subtract NSDate objects. If relative to local time (and the two endpoints are in different timezones or maybe only one in DST and one in standard time) then you must use NSCalendar/NSDateComponents or format both dates and parse the character strings. – Hot Licks Sep 14 '14 at 22:11
  • And *you* also have to decide whether 1/1/2014 23:00 to 1/3/2014 23:59 should be considered one day or two. – Hot Licks Sep 14 '14 at 22:14

2 Answers2

0

Dates are complicated.

If you want a difference in days, hours, minutes, seconds, that's easy: Convert everything to NSDate, calculate the difference in seconds, convert to days, hours, minutes, seconds.

Anything else, you need to first define what results you actually want. Today at 1am and 11pm is the same day, but today 11pm and tomorrow 1am are different days - even though in the first case the difference is 22 hours, in the second case just two hours. So you have to define what you want. You have to define for which case you want a result of "0 days" and for which case you want a result of "1 days".

And if you change time zones, some dates will move to a different day, some won't.

It's up to you to decide what result you want. In any case, I'd convert all dates to the relevant time zone, extract the day, and calculate days differences from that.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
  • Close. Definitely get NSDate objects, but then use the NSCalendar API (only in the header and WWDC videos since 2013) – uchuugaka Sep 15 '14 at 00:38
-2

You need to convert the date into epoch time.
- (NSTimeInterval)timeIntervalSince1970

After you do that you can use the below code, to find the time difference in seconds and compare them.


NSDate* date1 = [NSDate date];
NSDate* date2 = [NSDate date];
NSTimeInterval secs = [date1 timeIntervalSinceDate:date2];

if (secs < 0)
{
NSLog("less");
}
else if (secs > 0)
{
NSLog("greater");
}
else
{
NSLog(@"same");
return NSOrderedSame;
}

barryjones
  • 2,149
  • 1
  • 17
  • 21
  • 2
    An NSDate is *already* a time interval since a reference date. There is no need to convert it to a *unix* epoch time, which is the same thing with a different reference date. – quellish Sep 14 '14 at 22:05