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

时间:2011-06-02 11:44:33

标签: iphone datetime date nsdate nsdateformatter

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

9 个答案:

答案 0 :(得分:220)

你在这里,

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了解详情。

希望它有所帮助。

答案 1 :(得分: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];

答案 2 :(得分: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

答案 3 :(得分: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];

答案 4 :(得分: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已被弃用

答案 5 :(得分:6)

把它放在一个扩展中并过你的生活

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(它是以前答案的迭代,但解决了所有未来的应用程序项目)

答案 6 :(得分: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];

答案 7 :(得分: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);

答案 8 :(得分: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)")