CallMeGreeny CallMeGreeny - 1 year ago 143
iOS Question

Convert NSDate to an Integer

So my dilemma is that I can't figure out how to store the date only in an integer value (fetched using:

NSDate* date = [NSDate date]

I found this code online which seems similar to what I need (I thought that changing setDateFormat to @"dd" would've worked (but apparently it didn't)

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy"];

//Optionally for time zone conversions
[formatter setTimeZone:[NSTimeZone timeZoneWithName:@"..."]];

NSString *stringFromDate = [formatter stringFromDate:date];

Am I just forgetting something simple about integers?

Answer Source

Use dd in setDateFormat:.

This will give only date but in string.

After this you can convert the string to integer, with the help of integerValue.


NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd"];

NSString *stringFromDate = [formatter stringFromDate:[NSDate date]];
NSInteger integerDate = [stringFromDate integerValue];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download