iphone - 如何获取整数格式的 NSDate 日、月、年?

标签 iphone datetime date nsdate nsdateformatter

我想以整数形式获取 NSDate 的日、月和年部分,即如果日期是 1/2/1988,那么我应该分别获取 1、2 和 1988 作为整数。我怎样才能在 iOS 中做到这一点?我发现了类似的问题,但方法 descriptionWithCalendarFormat: 给出了警告,并且现在似乎已被弃用。

最佳答案

给你,

NSDate *currentDate = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:currentDate]; // Get necessary date components

 [components month]; //gives you month
 [components day]; //gives you day
 [components year]; // gives you year

您可以使用 NSDateComponents 来实现上述目的。

请访问this page了解详情。

关于iphone - 如何获取整数格式的 NSDate 日、月、年?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6214094/

相关文章:

ios - 如何提示编译器使用 UIView 子类

iPad 应用程序的 iOS 自定义 GridView 显示下载的图像

vba - 获取日期模式 yyyy-MM-dd HH :mm:ss z with VBA

python - Django:根据字段的状态注释 Sum Case When

excel - 日期的 COUNTIF 在当前周内,对于上周

sql - 对于仅包含年份的列,我应该使用什么类型?

php - Mysql和php中使用foreach显示两个数组的值并保存到数据库中

iphone - 如何在 iPhone 的 HTML5 网络应用程序中实现固定的标题和动画(如幻灯片)(如 Google Music 所做的那样)?

iphone - 如何在基于导航的 iOS 应用程序中访问比当前 View 上一级的 View ?

Pandas:如何在不使用for循环的情况下将日期列中每个条目的日期增加一定天数