iphone Get current year as string

后端 未结 5 1264
迷失自我
迷失自我 2021-02-01 01:33

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 ye

5条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-01 02:32

    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.

提交回复
热议问题