Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

thedollarhunter

macrumors member
Original poster
May 9, 2011
80
0
UK
Having just discovered NSDate I am trying to return a time interval (a typedef of long as a positive or negative number of seconds if I understand it correctly)
using the follow line of code

Code:
NSTimeInterval dateDiff = [[NSDate dateWithString:@"2011-11-17 00:00:00 +0000"] timeIntervalSinceNow];

It seems to work but when I build I see a Warning message that says:
NSDate may not respond to +dateWithString

What am I doing wrong?

Basically I want a check a given date to see if it is in the past or future.
 
Last edited:

jiminaus

macrumors 65816
Dec 16, 2010
1,449
1
Sydney
There is no mention of dateWithString: in the iOS NSDate Class Reference. It is is the the Mac OS X NSDate Class Reference, but it's use is discouraged. If it does in fact work in iOS, it's private API, and should be avoided.

You should send dateFromString: to an NSDateFormatter object instead, if your dates are in fact in a string.

If you dates aren't in a string but instead you have them as separate components, check out the NSDateComponents class.
 
Last edited:

thedollarhunter

macrumors member
Original poster
May 9, 2011
80
0
UK
Thanks for pointing that out, I will use dateFromString instead.

Another rookie question though, if I use NSDateFormatter to parse my string will it still work on devices with different regional settings?

Code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd MMM yyyy HH:mm:ss ZZ"];

NSDate *dateToCheck = [dateFormatter dateFromString:@"24 Dec 2011 00:00:00 -0000"];

NSTimeInterval dateDiff = [dateToCheck timeIntervalSinceNow];
 

jiminaus

macrumors 65816
Dec 16, 2010
1,449
1
Sydney
Thanks for pointing that out, I will use dateFromString instead.

Another rookie question though, if I use NSDateFormatter to parse my string will it still work on devices with different regional settings?

Code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd MMM yyyy HH:mm:ss ZZ"];

NSDate *dateToCheck = [dateFormatter dateFromString:@"24 Dec 2011 00:00:00 -0000"];

NSTimeInterval dateDiff = [dateToCheck timeIntervalSinceNow];

The user's locale and calendar are taken into consideration, so you want to set the locale before you set the format.

See Parsing Date String. In particular take heed of the advice to use the en_US_POSIX locale, not the en_US locale.
 

thedollarhunter

macrumors member
Original poster
May 9, 2011
80
0
UK
Thanks for the pointer, that link is just what I was looking for.

I've added the en_US_POSIX locale and tested it out by picking random International settings on the device (never knew iOS offered a Buddhist calendar) and it seems to work just fine.

Again, thank you for taking the time to help others, it is really appreciated.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.