Alex Rivera | Logout

iphone Get current year as string

Asked 2011-01-14T09:29:19.560
44

How do I get current year as string in Obj-C ?

Also how do I compare the same using another year value ?

Is it advisable to do a string comparision OR dirctly year-to-year comparision ?

Edit
Report

1 Answer

122
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy"];
NSString *yearString = [formatter stringFromDate:[NSDate date]];

// Swift
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy"
let year = dateFormatter.string(from: Date())

You can compare NSStrings via the -isEqualToString: method.

answered 2011-01-14T09:43:40.593

Your Answer