本文介绍了iPhone获取当前年份为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Obj-C中将当前年份作为字符串?

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 ?

推荐答案

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())

您可以通过-isEqualToString:方法比较NSString.

这篇关于iPhone获取当前年份为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 06:39