如何获取NSDate日,月和年的整数格式?


95

我想以NSDate整数形式获取日,月和年部分,即如果日期为1/2/1988,则我应该分别以整数形式获取1、2和1988。如何在iOS中执行此操作?我发现了类似的问题,但是方法descriptionWithCalendarFormat:发出警告,并且现在似乎已弃用。


如果仅需要一个整数,请参阅将NSDate转换为整数NSDate
Suragch

Answers:


223

这个给你,

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。

请访问此页面以获取详细信息。

希望能帮助到你。


1
+1用于提供正确答案的代码。但您应该提到该信息来自NSDateComponents,而不是NSDateFormatter。
黑蛙

@黑色为此表示歉意。更新了我的答案。
Janak Nirmal

1
@ Janak Nirmal写的具体点是您无法解释如何获取用户无法理解的日,月,年的字符串格式。
2015年

1
@JanakNirmal您应该解释它如何获得确切的价值。
2015年

3
为避免弃用警告:NSDateComponents *组件= [日历组件:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:currentDate];
罗德里戈·平托

17

是的,但是通过使用NSCalendar,我认为这将使您的工作变得更容易。

NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]];
NSInteger day = [components day];    
NSInteger month = [components month];
NSInteger year = [components year];

13

迅速

let components = NSCalendar.currentCalendar().components([.Day, .Month, .Year], fromDate: self)
let day = components.day
let month = components.month
let year = components.year

为了方便起见,可以将其放在NSDate扩展中并使其返回一个元组:

extension NSDate: Comparable {

    var dayMonthYear: (Int, Int, Int) {
        let components = NSCalendar.currentCalendar().components([.Day, .Month, .Year], fromDate: self)
        return (components.day, components.month, components.year)
    }
}

现在您只需要编写:

let (day, month, year) = date.dayMonthYear

例如,如果您只想获得年份,则可以编写:

let (_, _, year) = date.dayMonthYear

2
我喜欢你的方法。有人建议虽然是改变参数来命名(day: Int, month: Int, year: Int),这样你可以通过使用结果date.dayMonthYear.daydate.dayMonthYear.monthdate.dayMonthYear.year
Paul Peelen '16

9

您可以使用NSDateComponents获得此信息,

NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDate *currDate = [NSDate date];
NSDateComponents *dComp = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit ) 
                                      fromDate:currDate];

int day = [dComp day];
int month = [dComp month];
int year = [dComp year];

8

扩展它并过上自己的生活🍸🏖

extension Date{
    var day:Int {return Calendar.current.component(.day, from:self)}
    var month:Int {return Calendar.current.component(.month, from:self)}
    var year:Int {return Calendar.current.component(.year, from:self)}
}
Date().day//22
Date().month//4
Date().year//2017

Swift 3.0(这是先前答案的迭代,但是可以为您将来的所有应用程序项目解决)


7

对于像我这样直接从网络直接复制和粘贴的用户,这是iOS 8的一个小更新

NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents* components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:[NSDate date]];
[components day]; //Day
[components month];//Month
[components year];//Year

区别在于NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay NSMonthCalendarUnit | NSDayCalendarUnit | NSYearCalendarUnit | NSWeekdayCalendarUnit已弃用


2

你可以试试这个.......

Mycal = [NSCalendar currentCalendar];
today = [NSDate date];
compA =[Mycal components:(NSMonthCalendarUnit | NSDayCalendarUnit | NSYearCalendarUnit | NSWeekdayCalendarUnit) fromDate:today];     
FirstDateofMonth=[today dateByAddingTimeInterval:([compA day]-1)*(-24*60*60)];

compA =[Mycal components:(NSMonthCalendarUnit | NSDayCalendarUnit | NSYearCalendarUnit | NSWeekdayCalendarUnit) fromDate:FirstDateofMonth];
today = FirstDateofMonth;
compA = [Mycal components:(NSMonthCalendarUnit | NSDayCalendarUnit | NSYearCalendarUnit | NSWeekdayCalendarUnit) fromDate:today];
[compA setMonth:([compA month])];
[self DrawMonthInPortraitView];
[compA retain];

1
NSDate *currentDate = [NSDate date];
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents* components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:currentDate]; // Get necessary date components

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

NSString *temp=[NSString stringWithFormat:@"Months:%d Day:%d Year:%d",[components month],  [components day],[components year]];
NSLog(@"%@",temp);

1
let date = Date()
let calendar = Calendar.current

let year = calendar.component(.year, from: date)
let month = calendar.component(.month, from: date)
let day = calendar.component(.day, from: date)
print("hours = \(year):\(month):\(day)")

1
尽管此代码可以回答问题,但提供有关此代码为何和/或如何回答问题的其他上下文,可以提高其长期价值。
唐老鸭
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.